diff --git a/lab-4/parser.bison b/lab-4/parser.bison index babfbd9..c0c71ca 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -51,7 +51,6 @@ int yylex(); %token TOKEN_LEFT_PARENTHESIS %token TOKEN_RIGHT_PARENTHESIS %token TOKEN_DOT -%token TOKEN_COMMENT %token TOKEN_ADD %token TOKEN_SUB %token TOKEN_MULTIPLY @@ -65,7 +64,9 @@ int yylex(); %% file : statements -statements : statements statement +statements : statement_list + ; +statement_list : statement_list statement | statement ; statement : section @@ -85,13 +86,14 @@ type : TOKEN_KEYWORD_IDENTIFICATION | TOKEN_STOP | TOKEN_KEYWORD_DATA ; -simple_stmt : cbl_function - | cbl_function op_parms - | cbl_function assignment_stmt - | cbl_function op_parms assignment_stmt +simple_stmt : cbl_func_stmt | if_branch | perform_stmt ; +cbl_func_stmt : cbl_function + | cbl_function op_parms + | cbl_function assignment_stmt + | cbl_function op_parms assignment_stmt ; assignment_stmt : TOKEN_EQUAL ext_function | TOKEN_EQUAL function @@ -107,11 +109,13 @@ op_parms : op_parms TOKEN_ADD op_parms | op_parms TOKEN_EQUAL op_parms | TOKEN_SUB op_parms | TOKEN_LEFT_PARENTHESIS op_parms TOKEN_RIGHT_PARENTHESIS - | TOKEN_IDENT + | expr + | op_parms op_parms + ; +expr : TOKEN_IDENT | TOKEN_INTEGER | TOKEN_STRING | TOKEN_SPACE - | op_parms op_parms ; function : op_parms ; @@ -142,8 +146,8 @@ data_category : TOKEN_ALPHANUMERIC categry_contain : TOKEN_LEFT_PARENTHESIS TOKEN_INTEGER TOKEN_RIGHT_PARENTHESIS | TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS ; -complete_category: complete_category complete_category - | data_category categry_contain +complete_category: data_category categry_contain + | data_category categry_contain complete_category ; data_clause : TOKEN_COMPUTATION_LEVEL_0 | TOKEN_COMPUTATION_LEVEL_1 @@ -157,15 +161,18 @@ full_data_clause: data_clause data_clause ; simple_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_DOT ; -full_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category TOKEN_DOT - | TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category full_data_clause TOKEN_DOT - | TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category full_data_clause TOKEN_INTEGER TOKEN_DOT +complex_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE category_spec TOKEN_DOT + ; +category_spec : complete_category + | complete_category data_clauses + ; +data_clauses : full_data_clause + | full_data_clause TOKEN_INTEGER ; data_declaration: simple_decl - | full_decl + | complex_decl ; - %% void yyerror(const char* msg) { fprintf(stderr, "Error | Line: %d\n%s\n",yylineno,msg); diff --git a/lab-5/Makefile b/lab-5/Makefile index dfc7723..0361623 100644 --- a/lab-5/Makefile +++ b/lab-5/Makefile @@ -20,7 +20,7 @@ scanner.c: scanner.flex parser.h flex -oscanner.c scanner.flex parser.c parser.h: parser.bison - bison --defines=parser.h --output=parser.c -v parser.bison -Wconflicts-sr -Wcounterexamples + bison --defines=parser.h --output=parser.c -v parser.bison # clean causes all intermediate files to be deleted. diff --git a/lab-5/parser.bison b/lab-5/parser.bison index babfbd9..e536a2c 100644 --- a/lab-5/parser.bison +++ b/lab-5/parser.bison @@ -1,14 +1,41 @@ %{ #define YYDEBUG 1 #include + +#include +#include +#include +#include "expr.h" + +/* +YYSTYPE is the lexical value returned by each rule in a bison grammar. +By default, it is an integer. In this example, we are returning a pointer to an expression. +*/ + +#define YYSTYPE struct expr * + +/* +Clunky: Manually declare the interface to the scanner generated by flex. +*/ + +extern char *yytext; +extern int yylex(); extern int yylineno; void yyerror(const char*); -int yylex(); + + +/* +Clunky: Keep the final result of the parse in a global variable, +so that it can be retrieved by main(). +*/ + +struct expr * parser_result = 0; %} %debug %define parse.error detailed + %token TOKEN_EOF %token TOKEN_KEYWORD_IDENTIFICATION %token TOKEN_KEYWORD_DIVISION @@ -51,7 +78,6 @@ int yylex(); %token TOKEN_LEFT_PARENTHESIS %token TOKEN_RIGHT_PARENTHESIS %token TOKEN_DOT -%token TOKEN_COMMENT %token TOKEN_ADD %token TOKEN_SUB %token TOKEN_MULTIPLY @@ -65,7 +91,9 @@ int yylex(); %% file : statements -statements : statements statement +statements : statement_list + ; +statement_list : statement_list statement | statement ; statement : section @@ -85,13 +113,14 @@ type : TOKEN_KEYWORD_IDENTIFICATION | TOKEN_STOP | TOKEN_KEYWORD_DATA ; -simple_stmt : cbl_function - | cbl_function op_parms - | cbl_function assignment_stmt - | cbl_function op_parms assignment_stmt +simple_stmt : cbl_func_stmt | if_branch | perform_stmt ; +cbl_func_stmt : cbl_function + | cbl_function op_parms + | cbl_function assignment_stmt + | cbl_function op_parms assignment_stmt ; assignment_stmt : TOKEN_EQUAL ext_function | TOKEN_EQUAL function @@ -107,11 +136,13 @@ op_parms : op_parms TOKEN_ADD op_parms | op_parms TOKEN_EQUAL op_parms | TOKEN_SUB op_parms | TOKEN_LEFT_PARENTHESIS op_parms TOKEN_RIGHT_PARENTHESIS - | TOKEN_IDENT + | expr + | op_parms op_parms + ; +expr : TOKEN_IDENT | TOKEN_INTEGER | TOKEN_STRING | TOKEN_SPACE - | op_parms op_parms ; function : op_parms ; @@ -142,8 +173,8 @@ data_category : TOKEN_ALPHANUMERIC categry_contain : TOKEN_LEFT_PARENTHESIS TOKEN_INTEGER TOKEN_RIGHT_PARENTHESIS | TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS ; -complete_category: complete_category complete_category - | data_category categry_contain +complete_category: data_category categry_contain + | data_category categry_contain complete_category ; data_clause : TOKEN_COMPUTATION_LEVEL_0 | TOKEN_COMPUTATION_LEVEL_1 @@ -157,15 +188,18 @@ full_data_clause: data_clause data_clause ; simple_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_DOT ; -full_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category TOKEN_DOT - | TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category full_data_clause TOKEN_DOT - | TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category full_data_clause TOKEN_INTEGER TOKEN_DOT +complex_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE category_spec TOKEN_DOT + ; +category_spec : complete_category + | complete_category data_clauses + ; +data_clauses : full_data_clause + | full_data_clause TOKEN_INTEGER ; data_declaration: simple_decl - | full_decl + | complex_decl ; - %% void yyerror(const char* msg) { fprintf(stderr, "Error | Line: %d\n%s\n",yylineno,msg); diff --git a/lab-5/scanner.flex b/lab-5/scanner.flex index 4b7a148..ca9acac 100644 --- a/lab-5/scanner.flex +++ b/lab-5/scanner.flex @@ -1,5 +1,5 @@ %{ -#include "token.h" +#include "parser.h" %} NAME [a-zA-Z]([a-zA-Z0-9_-]*[a-zA-Z0-9])? DIGIT [0-9]+