Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include "attr.h"
- int yylex();
- void yyerror(char * s);
- struct Stack *st;
- struct Stack *ids;
- #include "symtab.h"
- %}
- %union {tokentype token;
- char *type;
- struct Stack *idstack;
- int iconst;
- node *attr;
- }
- %token PROG PERIOD VAR ARRAY RANGE OF
- %token INT BOOLEAN WRITELN THEN ELSE IF
- %token BEG END ASG
- %token EQ NEQ LT LEQ OR EXOR AND NOT TRUE FALSE
- %token WHILE DO FOR
- %token <token> ID ICONST
- %type <attr> stype type
- %type <idstack> IDlist
- %type <iconst> integer_constant
- %start program
- %nonassoc EQ NEQ LT LEQ
- %left '+' '-' OR EXOR
- %left '*' AND
- %right NOT
- %nonassoc THEN
- %nonassoc ELSE
- %%
- program : PROG ID ';' block PERIOD { printf("\n\n Done with compiling program %s\n", $2.str); }
- ;
- block : variables cmpdstmt
- ;
- variables : VAR vardcls
- | /* epsilon */
- ;
- vardcls : vardcls vardcl ';'
- | vardcl ';'
- | error ';' { yyerror("***Error: illegal variable declaration\n"); }
- ;
- vardcl : IDlist ':' type { STInsertList(st, ids, $3->type, level); }
- ;
- IDlist : IDlist ',' ID { SPushT(ids, $3); }
- | ID { SPushT(ids, $1); }
- ;
- type : ARRAY '[' integer_constant RANGE integer_constant ']' OF stype { $$ = $8 }
- | stype { $$ = $1 }
- ;
- stype : INT { $$ = "INT" }
- | BOOLEAN { $$ = "BOOLEAN" }
- ;
- stmtlist : stmtlist ';' stmt
- | stmt
- | error ';' { yyerror("***Error: illegal statement\n");}
- ;
- stmt : ifstmt
- | astmt
- | wstmt
- | fstmt
- | cmpdstmt
- | writestmt
- ;
- wstmt : WHILE condexp DO stmt
- ;
- fstmt : FOR ID ASG constant ',' constant DO astmt
- ;
- ifstmt : ifhead THEN stmt ELSE stmt
- | ifhead THEN stmt
- ;
- ifhead : IF condexp
- ;
- cmpdstmt : BEG stmtlist END
- ;
- writestmt : WRITELN '(' exp ')'
- ;
- astmt : lvalue ASG exp
- exp : rvalue { }
- | exp '+' exp { }
- | exp '-' exp { }
- | exp '*' exp { }
- | exp AND exp { }
- | exp OR exp { }
- | exp EXOR exp { }
- | NOT exp { }
- | '(' exp ')' { }
- | constant { }
- | error { yyerror("***Error: illegal expression\n"); }
- ;
- condexp : exp NEQ exp { }
- | exp EQ exp { }
- | exp LT exp { }
- | exp LEQ exp { }
- | ID { }
- | boolean_constant { }
- | error { yyerror("***Error: illegal conditional expression\n"); }
- ;
- lvalue : ID
- | ID '[' exp ']'
- ;
- rvalue : ID
- | ID '[' exp ']'
- ;
- constant : integer_constant
- | boolean_constant
- ;
- integer_constant: ICONST { $$ = atoi($1) }
- ;
- boolean_constant: TRUE
- | FALSE
- ;
- %%
- struct StackTokenNode{
- tokentype data;
- void *next;
- int rc;
- };
- typedef struct StackTokenNode stNode;
- int SPushT(sPtr s, tokentype data){
- stNode *new = malloc(sizeof(sNode));
- new->data = data;
- new->next = NULL;
- new->rc = 0;
- if (s->size == 0){
- s->top = new;
- s->bottom = new;
- s->size++;
- return 0;
- }
- new->next = s->top;
- s->top = new;
- s->size++;
- return 0;
- }
- void yyerror(char* s) {
- fprintf(stderr,"%s\n",s);
- }
- int main() {
- printf("\n CS415 Front-End Compiler\n");
- printf(" Project 2, Spring 2013\n\n");
- printf("1\t");
- st = STCreate();
- ids = SCreate(CompareStrings);
- level = 0;
- yyparse();
- vNode *vn = SPop(st);
- while (vn != NULL){
- printf("%s %s %d", vn->id, vn->type, vn->level);
- vn = SPop(st);
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement