Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %%
  2. %term
  3.     EOF
  4.   | ID of string
  5.   | INT of int | STRING of string  (*literals*)
  6.   | COMMA | COLON | SEMICOLON | LPAREN | RPAREN | LBRACK | RBRACK (*punctuation*)
  7.   | LBRACE | RBRACE | DOT (*record operators*)
  8.   | UNARY | PLUS | MINUS | TIMES | DIVIDE | EQ | NEQ | LT | LE | GT | GE (*arithmetic operators*)
  9.   | AND | OR | ASSIGN
  10.   | ARRAY | IF | THEN | ELSE | NO_ELSE | WHILE | FOR | TO | DO | LET | IN | END | OF (*control flow*)
  11.   | BREAK | NIL (*more control flow*)
  12.   | FUNCTION | VAR | TYPE
  13.  
  14. %nonterm  exp | program | decs | dec | tydec | vardec | fundec | ty | tyfields | tyfield
  15.     | lvalue | lval | exp_sequence | sequence | seq | fun_args | args | recs | rec | rec_tail | rec_node | record | control_flow | function_call | boolean | arithmetic | comparison | assign | array
  16.  
  17. %pos int
  18. %verbose
  19. %start program
  20. %eop EOF
  21. %noshift EOF
  22.  
  23. %name Tiger
  24.  
  25. %keyword WHILE FOR TO BREAK LET IN END FUNCTION VAR TYPE ARRAY IF THEN ELSE
  26.     DO OF NIL
  27.  
  28. %prefer THEN ELSE LPAREN
  29.  
  30. %value ID ("bogus")
  31. %value INT (1)
  32. %value STRING ("")
  33.  
  34. (*Setting explicit precedence follows*)
  35.  
  36. (* We want to shift when there is an exp of the following form
  37.  * if e1 then if e2 then s1 else s2
  38.  * shifting is preferred to reducing to avoid
  39.  * the dangling else problem.
  40.  * Hence THEN has lower precedence than ELSE => shift will happen, not reduce.
  41.  * idea borrowed from :  
  42.  * https://www.gnu.org/software/bison/manual/html_node/Precedence-Only.html#Precedence-Only
  43.  *)
  44. %nonassoc NO_ELSE
  45. %nonassoc ELSE
  46.  
  47. %right OF (*OF is right associative in array OF*)
  48. %nonassoc DO
  49. %nonassoc ASSIGN
  50. %nonassoc EQ NEQ LT GT LE GE
  51.  
  52. (*A&B|C&D should reduce to ((A&B|(C&D), and not (A&(B|C)&D)*)
  53. %left OR
  54. %left AND
  55.  
  56. %left PLUS MINUS
  57. %left TIMES DIVIDE
  58. %left UNARY (*unary minus, highest precedence*)
  59.  
  60. %%
  61.  
  62. program : exp               ()
  63.  
  64.    
  65. (*Declarations*)
  66. decs : (*epsilon*) ()
  67.      | dec decs    ()
  68.            
  69. dec : tydec    ()
  70.     | vardec   ()
  71.     | fundec   ()
  72.  
  73. (*Data types*)
  74. tydec : TYPE ID EQ ty () (*type type-id = ty*)
  75. (*ID : ID               ()    *)  
  76.  
  77. ty : ID ()
  78.    | LBRACE tyfields RBRACE     ()
  79.    | ARRAY OF ID            ()
  80.  
  81. tyfield : COMMA ID COLON ID     () (*{,id : type-id}*)
  82.        
  83. tyfields : (*epsilon*)              ()
  84.      | ID COLON ID tyfield      ()
  85.  
  86. (*Variables*)
  87. vardec : VAR ID ASSIGN exp              () (*var id := exp*)
  88.        | VAR ID COLON ID ASSIGN exp     () (*var id:type-id := exp*)
  89.  
  90. (*Functions*)
  91. fundec : FUNCTION ID LPAREN tyfields RPAREN EQ exp              ()
  92.        | FUNCTION ID LPAREN tyfields RPAREN COLON ID EQ exp     ()
  93.  
  94.     (*lvalue with 'apparently redundant' production as in textbook*)
  95. lvalue: ID  ()
  96.       | lvalue DOT ID  ()
  97.       | lvalue LBRACK exp RBRACK ()
  98.       | ID LBRACK exp RBRACK ()
  99.  
  100.      
  101. (*Expressions*)
  102.  
  103.         (*Some useful production shorthands*)
  104. sequence : LPAREN exp_sequence RPAREN ()
  105. exp_sequence : (*epsilon*) ()
  106.          | exp seq     ()
  107.  
  108. seq : (*epsilon*)                () (*an exp sequence can be empty*)
  109.     | SEMICOLON exp exp_sequence () (*exps separated by semicolon - to be used below*)
  110.  
  111.  
  112. (* Productions for exp *)        
  113. exp : lvalue                () (*lval*)
  114.     | sequence              () (*sequence of expressions*)
  115.     | INT               () (*integer literal*)
  116.     | STRING                () (*string literal *)
  117.     | function_call         ()
  118.     | arithmetic            ()
  119.     | comparison            ()
  120.     | boolean               ()
  121.     | control_flow          ()
  122.     | array             ()
  123.     | assign                ()
  124.     | record                ()
  125.     | NIL               () (*nil - reserved keyword *)
  126.     (*| LPAREN exp RPAREN           () paren*)
  127.      
  128. assign: lvalue ASSIGN exp           () (*assignment*)
  129.        
  130. comparison : exp EQ exp         ()
  131.     | exp NEQ exp           ()
  132.     | exp GT exp            ()
  133.     | exp LT exp            ()
  134.     | exp LE exp            ()
  135.     | exp GE exp            ()
  136.      
  137. arithmetic : MINUS exp %prec UNARY  ()(* negation - highest precedence! *)
  138.     | exp PLUS exp          ()
  139.     | exp MINUS exp         ()
  140.     | exp DIVIDE exp            ()
  141.     | exp TIMES exp         ()
  142.  
  143. boolean : exp AND exp           ()
  144.     | exp OR exp            ()
  145.      
  146.  
  147. function_call : ID LPAREN fun_args RPAREN       ()
  148. (* Helper production for function arguments.
  149.  * Function arguments can be empty,
  150.  * or of the form (exp{, exp}
  151. *)
  152. fun_args: (*epsilon*) ()
  153.     | exp args    ()
  154. (*production for args -> {,exp}*)
  155. args : (*empty*)      ()
  156.      | COMMA exp args ()
  157.  
  158.  
  159. control_flow : IF exp THEN exp ELSE exp     () (*if-then-else*)
  160.     | IF exp THEN exp %prec NO_ELSE     () (*if-then*)
  161.     | WHILE exp DO exp          () (*while*)
  162.     | FOR ID ASSIGN exp TO exp DO exp   ()(*for*)
  163.     | BREAK                 () (*break*)
  164.     | LET decs IN sequence END  () (*let-in-end*)
  165.  
  166. record : ID LBRACE rec_node rec_tail RBRACE     () (*record creation*)
  167.  
  168. (* Helper production for record creation:
  169.  * type-id {id=exp{, id=exp}}
  170.  *
  171.  *)
  172. rec_node : ID EQ exp  ()
  173. rec_tail   : (*empty*) ()
  174.        | COMMA rec_node rec_tail ()
  175.          
  176. array : ID LBRACK exp RBRACK OF exp () (*array creation*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement