#include <stdlib.h>
%}
-%start Program
-%token FUNC END STRUCT VAR IF THEN ELSE WHILE DO RETURN OR NOT
-%token ID NUM ASSIGN GREATER
+%start Program
+%token FUNC END STRUCT VAR IF THEN ELSE WHILE DO RETURN OR NOT
+%token ID NUM ASSIGN GREATER
%%
-Program: Funcdef ';' Program
- | Structdef ';' Program
- |
- ;
-
-Funcdef: FUNC ID '(' Pars ')' Stats END
- | FUNC ID '(' ')' Stats END
- ;
-
-Structdef: STRUCT Ids END
- ;
-
-Ids: ID Ids
- |
- ;
-
-Pars: Pars ',' ID
- | ID
- ;
-
-Stats: Stat ';' Stats
- |
- ;
-
-Stat: VAR ID ASSIGN Expr
- | Lexpr ASSIGN Expr
- | IF Bool THEN Stats END
- | IF Bool THEN Stats ELSE Stats END
- | WHILE Bool DO Stats END
- | Call
- | RETURN Expr
- ;
-
-Lexpr: ID
- | Field
- ;
-
-Expr: '-' Term
- | Term
- | Term Plusterm
- | Term Malterm
- ;
-
-Plusterm: '+' Term Plusterm
- | '+' Term
- ;
-
-Malterm: '*' Term Malterm
- | '*' Term
- ;
-
-Term: '(' Expr ')'
- | ID
- | NUM
- | Call
- | Field
- ;
-
-Bool: Bterm
- | Bterm Orterm
- | NOT Bterm
- ;
-
-Orterm: OR Bterm Orterm
- | OR Bterm
- ;
-
-Bterm: Term GREATER Term
- | Term '=' Term
- | '(' Bool ')'
- ;
-
-Field: Term '.' ID
- ;
-
-Call: ID '(' Exprs ')'
- | ID '(' ')'
- ;
-
-Exprs: Expr
- | Exprs ',' Expr
- ;
+Program:
+ Funcdef ';' Program
+ | Structdef ';' Program
+ |
+ ;
+
+Funcdef:
+ FUNC ID '(' Pars ')' Stats END
+ | FUNC ID '(' ')' Stats END
+ ;
+
+Structdef:
+ STRUCT Ids END
+ ;
+
+Ids:
+ ID Ids
+ |
+ ;
+
+Pars:
+ Pars ',' ID
+ | ID
+ ;
+
+Stats:
+ Stat ';' Stats
+ |
+ ;
+
+Stat:
+ VAR ID ASSIGN Expr
+ | Lexpr ASSIGN Expr
+ | IF Bool THEN Stats END
+ | IF Bool THEN Stats ELSE Stats END
+ | WHILE Bool DO Stats END
+ | Call
+ | RETURN Expr
+ ;
+
+Lexpr:
+ ID
+ | Field
+ ;
+
+Expr:
+ '-' Term
+ | Term
+ | Term Plusterm
+ | Term Malterm
+ ;
+
+Plusterm:
+ '+' Term Plusterm
+ | '+' Term
+ ;
+
+Malterm:
+ '*' Term Malterm
+ | '*' Term
+ ;
+
+Term:
+ '(' Expr ')'
+ | ID
+ | NUM
+ | Call
+ | Field
+ ;
+
+Bool:
+ Bterm
+ | Bterm Orterm
+ | NOT Bterm
+ ;
+
+Orterm:
+ OR Bterm Orterm
+ | OR Bterm
+ ;
+
+Bterm:
+ Term GREATER Term
+ | Term '=' Term
+ | '(' Bool ')'
+ ;
+
+Field:
+ Term '.' ID
+ ;
+
+Call:
+ ID '(' Exprs ')'
+ | ID '(' ')'
+ ;
+
+Exprs:
+ Expr
+ | Exprs ',' Expr
+ ;
%%
extern int yylex();
extern int yylineno;
-int yyerror(char *error_text) {
- fprintf(stderr,"Line %i: %s\n",yylineno, error_text);
+int yyerror(char *error_text)
+{
+ fprintf(stderr,"Line %i: %s\n", yylineno, error_text);
exit(2);
}
-int main(int argc, char **argv) {
-/* yydebug=1; */
+int main(int argc, char **argv)
+{
+ #if 0
+ yydebug=1;
+ #endif
yyparse();
return 0;
}
#include <string.h>
#include "parser.h"
-KEYWORD func|end|struct|var|if|then|else|while|do|return|or|not
-SPECIAL_CHAR \;|\.|\(|\)|\,|\:=|\-|\+|\*|>=|=
-IDENTIFIER [[:alpha:]_][[:alnum:]_]*
-NUMBER_HEX [0-9][0-9A-Fa-f]*H
-NUMBER_DEC [0-9]+
-WHITESPACE [\t\n\r ]
-COMMENT_START \(\*
-COMMENT_END \*\)
+KEYWORD func|end|struct|var|if|then|else|while|do|return|or|not
+SPECIAL_CHAR \;|\.|\(|\)|\,|\:=|\-|\+|\*|>=|=
+IDENTIFIER [[:alpha:]_][[:alnum:]_]*
+NUMBER_HEX [0-9][0-9A-Fa-f]*H
+NUMBER_DEC [0-9]+
+WHITESPACE [\t\n\r ]
+COMMENT_START \(\*
+COMMENT_END \*\)
%x COMMENT
%option yylineno
%%
-{COMMENT_START} BEGIN(COMMENT);
+{COMMENT_START} BEGIN(COMMENT);
-<COMMENT>{COMMENT_END} BEGIN(INITIAL);
+<COMMENT>{COMMENT_END} BEGIN(INITIAL);
-<COMMENT>{COMMENT_START} fprintf(stderr, "Possibly nested comment on line %i\n", yylineno);
+<COMMENT>{COMMENT_START} fprintf(stderr, "Possibly nested comment on line %i\n", yylineno);
-<COMMENT><<EOF>> { fprintf(stderr, "Unterminated comment.\n"); exit(1); }
+<COMMENT><<EOF>> {
+ fprintf(stderr, "Unterminated comment.\n");
+ exit(1);
+}
-<COMMENT>{WHITESPACE} /* ignore */
+<COMMENT>{WHITESPACE} /* ignore */
-<COMMENT>. /* ignore everything inside comment */
+<COMMENT>. /* ignore everything inside comment */
-func return(FUNC);
-end return(END);
-struct return(STRUCT);
-var return(VAR);
-if return(IF);
-then return(THEN);
-else return(ELSE);
-while return(WHILE);
-do return(DO);
-return return(RETURN);
-or return(OR);
-not return(NOT);
+func return(FUNC);
+end return(END);
+struct return(STRUCT);
+var return(VAR);
+if return(IF);
+then return(THEN);
+else return(ELSE);
+while return(WHILE);
+do return(DO);
+return return(RETURN);
+or return(OR);
+not return(NOT);
-{IDENTIFIER} return(ID);
+{IDENTIFIER} return(ID);
-{NUMBER_DEC} return(NUM);
-{NUMBER_HEX} return(NUM);
-
-\:= return(ASSIGN);
->= return(GREATER);
-{SPECIAL_CHAR} return(yytext[0]);
+{NUMBER_DEC} return(NUM);
+{NUMBER_HEX} return(NUM);
-{WHITESPACE} /* ignore */
+\:= return(ASSIGN);
+>= return(GREATER);
+{SPECIAL_CHAR} return(yytext[0]);
-. { fprintf(stderr, "Lexical error on line %i\n", yylineno); exit(1); }
+{WHITESPACE} /* ignore */
-%%
+. {
+ fprintf(stderr, "Lexical error on line %i\n", yylineno);
+ exit(1);
+}
+%%