added skeleton rules and grammar to bison file
This commit is contained in:
125
grammar.y
125
grammar.y
@ -10,7 +10,8 @@
|
||||
extern char* yytext;
|
||||
extern int yychar;
|
||||
SymbolTable * st;
|
||||
|
||||
char* cur_value;
|
||||
char* cur_type;
|
||||
%}
|
||||
|
||||
%token ID 101
|
||||
@ -61,12 +62,122 @@
|
||||
%token COMMENT 700
|
||||
%%
|
||||
|
||||
start: /*empty for now*/
|
||||
OPTIONAL;
|
||||
program:
|
||||
prototype_or_definition_list;
|
||||
|
||||
OPTIONAL:
|
||||
L_BRACKET {printf("success");};
|
||||
prototype_or_definition_list:
|
||||
prototype prototype_or_definition_list
|
||||
| definition prototype_or_definition_list
|
||||
| prototype
|
||||
| definition
|
||||
;
|
||||
prototype:
|
||||
L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID;
|
||||
|
||||
definition:
|
||||
TYPE ID COLON dblock
|
||||
| TYPE ID COLON constant ARROW ID
|
||||
| TYPE ID COLON ID ARROW ID
|
||||
| ID parameter ASSIGN sblock
|
||||
;
|
||||
|
||||
parameter:
|
||||
L_PAREN ID R_PAREN
|
||||
| AS L_PAREN idlist R_PAREN
|
||||
;
|
||||
|
||||
idlist:
|
||||
ID COMMA idlist
|
||||
|ID
|
||||
;
|
||||
|
||||
sblock:
|
||||
L_BRACE {st = CreateScope(st,1,1);} statement_list {st = getParent(st);} R_BRACE
|
||||
| L_BRACE {st = CreateScope(st,1,1);} dblock statement_list {st = getParent(st);} R_BRACE
|
||||
;
|
||||
|
||||
dblock:
|
||||
L_BRACKET declaration_list R_BRACKET;
|
||||
|
||||
declaration_list:
|
||||
declaration {CreateEntry(st,cur_type,cur_value);} SEMI_COLON declaration_list
|
||||
| declaration {CreateEntry(st,cur_type,cur_value);}
|
||||
;
|
||||
|
||||
declaration:
|
||||
ID COLON ID {cur_value = strdup($1);cur_type = strdup($3);};
|
||||
|
||||
statement_list:
|
||||
compound_statement statement_list
|
||||
| compound_statement
|
||||
| simple_statement SEMI_COLON statement_list
|
||||
| simple_statement SEMI_COLON
|
||||
;
|
||||
|
||||
compound_statement:
|
||||
WHILE L_PAREN expression R_PAREN sblock
|
||||
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock
|
||||
| sblock
|
||||
;
|
||||
|
||||
simple_statement:
|
||||
assignable ASSIGN expression
|
||||
| RETURN expression
|
||||
;
|
||||
|
||||
assignable:
|
||||
ID
|
||||
| assignable ablock
|
||||
| assignable DOT ID
|
||||
;
|
||||
|
||||
expression:
|
||||
constant
|
||||
| UnaryOperator expression
|
||||
| assignable
|
||||
| expression binaryOperator expression
|
||||
| L_PAREN expression R_PAREN
|
||||
| memOp assignable
|
||||
;
|
||||
|
||||
ablock:
|
||||
L_PAREN argument_list R_PAREN;
|
||||
|
||||
argument_list:
|
||||
expression COMMA argument_list
|
||||
| expression
|
||||
;
|
||||
|
||||
UnaryOperator:
|
||||
SUB_OR_NEG
|
||||
| NOT
|
||||
;
|
||||
|
||||
memOp:
|
||||
RESERVE
|
||||
| RELEASE
|
||||
;
|
||||
|
||||
binaryOperator:
|
||||
ADD
|
||||
| SUB_OR_NEG
|
||||
| MUL
|
||||
| DIV
|
||||
| REM
|
||||
| AND
|
||||
| OR
|
||||
| LESS_THAN
|
||||
| EQUAL_TO
|
||||
;
|
||||
|
||||
constant:
|
||||
C_STRING
|
||||
| C_INTEGER
|
||||
| C_NULL
|
||||
| C_CHARACTER
|
||||
| C_TRUE
|
||||
| C_FALSE
|
||||
;
|
||||
//
|
||||
%%
|
||||
|
||||
@ -75,11 +186,13 @@ void yyerror(const char *err) {
|
||||
}
|
||||
|
||||
int main() {
|
||||
cur_value[1] = "";
|
||||
cur_type[1] = "";
|
||||
st=CreateScope(NULL,1,1);
|
||||
int a;
|
||||
while ((a = yyparse()) != EOF){
|
||||
printf("%d = a: yytext = %s: yychar = %d\n", a, yytext, yychar);
|
||||
if(yytext[0] == '!'){
|
||||
if(yytext[0] == '?'){
|
||||
print_symbol_table(getAncestor(st),stdout);
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user