113 lines
2.5 KiB
Text
113 lines
2.5 KiB
Text
/* Require bison minimal version */
|
|
%require "3.2"
|
|
|
|
/* write out a header file containing the token defines */
|
|
%header
|
|
|
|
// Code for the header file generated by bison
|
|
%code requires {
|
|
typedef void* yyscan_t;
|
|
|
|
struct parser_state {
|
|
unsigned long num_fully_contained;
|
|
unsigned long num_partial_overlaps;
|
|
};
|
|
}
|
|
|
|
// Code for the c file
|
|
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "parser.h"
|
|
#include "lexer.h"
|
|
|
|
void yyerror(struct parser_state* state, yyscan_t scanner, const char* msg) {
|
|
(void)state;
|
|
(void)scanner;
|
|
fprintf(stderr, "\033[93mSyntax Error: %s\033[0m\n", msg);
|
|
}
|
|
%}
|
|
|
|
// define a reentrant parser
|
|
%define api.pure
|
|
|
|
// enable parser tracing and detailed error messages (plus Lookahead Correction)
|
|
%define parse.trace
|
|
%define parse.error detailed
|
|
%define parse.lac full
|
|
|
|
%lex-param { yyscan_t scanner }
|
|
%parse-param { struct parser_state* state }
|
|
%parse-param { yyscan_t scanner }
|
|
|
|
%union {
|
|
unsigned long num;
|
|
}
|
|
|
|
|
|
%start input
|
|
%token NEWLINE MINUS SEP
|
|
%token <num> NUMBER
|
|
%term END_OF_FILE
|
|
|
|
%%
|
|
|
|
input
|
|
: line input
|
|
| END_OF_FILE { return 0; }
|
|
;
|
|
|
|
line
|
|
: section_list NEWLINE
|
|
| NEWLINE
|
|
;
|
|
|
|
section_list
|
|
: NUMBER MINUS NUMBER SEP NUMBER MINUS NUMBER {
|
|
if ($1 <= $5) {
|
|
if ($3 >= $7) {
|
|
// pair 1 contains pair 2 fully
|
|
state->num_fully_contained++;
|
|
}
|
|
if ($3 >= $5) {
|
|
state->num_partial_overlaps++;
|
|
}
|
|
} else if ($5 <= $1) {
|
|
if ($7 >= $3) {
|
|
// pair 2 contains pair 1 fully
|
|
state->num_fully_contained++;
|
|
}
|
|
if ($7 >= $1) {
|
|
state->num_partial_overlaps++;
|
|
}
|
|
}
|
|
}
|
|
;
|
|
|
|
%%
|
|
|
|
int main(void) {
|
|
struct parser_state parser_state;
|
|
parser_state.num_fully_contained = 0;
|
|
parser_state.num_partial_overlaps = 0;
|
|
|
|
yyscan_t scanner;
|
|
|
|
if (yylex_init(&scanner)) {
|
|
fprintf(stderr, "\033[91m[error] Could not initialize lexer\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
if (yyparse(&parser_state, scanner)) {
|
|
// TODO: Error handling https://www.gnu.org/software/bison/manual/html_node/Parser-Function.html
|
|
// error during parse occured
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
yylex_destroy(scanner);
|
|
|
|
printf("Number of fully contained pairs: %lu\n", parser_state.num_fully_contained);
|
|
printf("Number of partial overlaps: %lu\n", parser_state.num_partial_overlaps);
|
|
|
|
return 0;
|
|
}
|