fix bison parser in lab-5
This commit is contained in:
parent
282aeafa54
commit
a807c61029
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,41 @@
|
|||
%{
|
||||
#define YYDEBUG 1
|
||||
#include <stdio.h>
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
|
|
|
|||
|
|
@ -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]+
|
||||
|
|
|
|||
Loading…
Reference in New Issue