Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %skeleton "lalr1.cc"
- %defines
- %define api.value.type variant
- %define api.token.constructor
- %code requires{
- #include "Node.hh"
- }
- %code{
- extern FILE yyin;
- unsigned int total = 0;
- #include <string>
- #define YY_DECL yy::parser::symbol_type yylex()
- YY_DECL;
- Node root;
- int Node::id = 0;
- }
- %token <std::string> SPACE
- %token <std::string> EQUAL
- %token <std::string> NUMBER
- %token <std::string> ADD
- %token <std::string> SUB
- %token <std::string> MULT
- %token <std::string> DIV
- %token <std::string> ELE
- %token <std::string> NEWLINE
- %token <std::string> START
- %token <std::string> FINISH
- %token <std::string> VAR
- %token <std::string> PRINT
- %token <std::string> COMMA
- %token <std::string> CITE
- %token <std::string> FOR
- %token <std::string> DO
- %token <std::string> ENDLOOP
- %token <std::string> READ
- %token <std::string> TRUE
- %token <std::string> FALSE
- %token <std::string> MOD
- %token <std::string> EQU
- %token <std::string> IF
- %token <std::string> THEN
- %token <std::string> LIST
- %token <std::string> LISTSTART
- %token <std::string> LISTEND
- %token <std::string> LISTRANGESTART
- %token <std::string> LISTRANGEEND
- %token <std::string> REPEAT
- %token <std::string> UNTIL
- %token <std::string> MORE
- %token <std::string> LESS
- %token <std::string> FUNCTION
- %token <std::string> ELSE
- //binop precedence
- %left MORE LESS EQU COMMA
- %left ADD SUB
- %left MULT DIV MOD
- %left ELE
- %type <Node> chunk
- %type <Node> exp
- %type <Node> optblank
- %type <Node> block
- %type <Node> stat
- %type <Node> prefixexp
- %type <Node> field
- %type <Node> var
- %type <Node> functioncall
- %type <Node> explist
- %type <Node> args
- %token END 0 "end of file"
- %%
- block : chunk {$$ = Node("BLOCK", "");
- $$.children.push_back($1);
- root = $$;};
- | block NEWLINE chunk { $$ = $1;
- $$.children.push_back($3);
- root = $$;}
- chunk : { $$ = Node("CHUNK", "empty");}
- | stat { $$ = Node("CHUNK","has stat");
- $$.children.push_back($1);};
- stat : field { $$ = $1; }
- optblank : {};
- | optblank SPACE {};
- | optblank READ {};
- var : optblank VAR optblank {$$ = Node("VAR",$2);};
- explist : exp {$$ = $1;};
- | explist COMMA optblank exp {$$ = $1;};
- exp : optblank NUMBER optblank {$$ = Node("NUMBER",$2);};
- | prefixexp {$$ = $1;};
- | functioncall {$$ = $1;};
- | exp MORE exp {$$ = Node("BINOP",">");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- | exp LESS exp {$$ = Node("BINOP","<");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- | exp EQU exp {$$ = Node("BINOP","==");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- | exp ADD exp {$$ = Node("BINOP","+");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- | exp SUB exp {$$ = Node("BINOP","-");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- | exp MULT exp {$$ = Node("BINOP","*");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- | exp DIV exp {$$ = Node("BINOP","/");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- | exp MOD exp {$$ = Node("BINOP","%");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- | exp ELE exp {$$ = Node("BINOP","^");
- $$.children.push_back($1);
- $$.children.push_back($3);};
- args : START explist END {$$ = $2;};
- prefixexp : var {$$ = $1;};
- | optblank START exp FINISH optblank {$$ = $3;};
- functioncall : prefixexp args {$$ = $1;};
- field : exp {$$ = $1;}
- | var EQUAL exp {$$ = Node("Assignment", "");
- $$.children.push_back($1);
- $$.children.push_back($3);};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement