Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
154
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.   | PLUS | MINUS | TIMES | DIVIDE | EQ | NEQ | LT | LE | GT | GE (*arithmetic operators*)
  9.   | AND | OR | ASSIGN | UMINUS (*unary? *)
  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 | seq | fun_args | args | recs | rec
  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 UMINUS (*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.  
  95. (*lvalue*)
  96. lvalue : ID             ()
  97.        | lval               ()
  98.        | lval LBRACK exp RBRACK ()
  99. (*Nested "dot" calls need to be supported *)
  100. lval : ID DOT ID        ()  
  101.      | lval DOT ID      ()
  102.      | ID LBRACK exp RBRACK     ()
  103.      
  104. (*Expressions*)
  105.  
  106. (*Some useful production shorthands*)
  107. exp_sequence : (*epsilon*) ()
  108.          | exp seq     ()
  109.  
  110. seq : (*epsilon*)                () (*an exp sequence can be empty*)
  111.     | SEMICOLON exp exp_sequence () (*exps separated by semicolon - to be used below*)
  112.  
  113.        
  114. (* Helper production for function arguments.
  115.  * Function arguments can be empty,
  116.  * or of the form (exp{, exp}
  117. *)
  118. fun_args: (*epsilon*) ()
  119.     | exp args    ()
  120. (*production for args -> {,exp}*)
  121. args : (*empty*)      ()
  122.      | COMMA exp args ()
  123.  
  124. (* Helper production for record creation:
  125.  * type-id {id=exp{, id=exp}}
  126.  *
  127.  *)
  128. recs : (*epsilon*)        ()  (*record can be empty*)
  129.      | ID EQ exp rec  () (* id=exp, id=exp, etc.*) (*30*)
  130. (*production for rec -> {, id=exp}*)
  131. rec : (*epsilon*)         ()
  132.     | COMMA ID EQ exp rec () (*, id=exp, id=exp, etc.*)
  133.          
  134. (* Productions for exp *)        
  135. exp : lvalue                () (*lval*)
  136.     | NIL               () (*nil - reserved keyword *)
  137.     | LPAREN exp_sequence RPAREN    () (*sequence of expressions*)
  138.     | INT               () (*integer literal*)
  139.     | STRING                () (*string literal *)
  140.     | MINUS exp %prec UMINUS        () (*negation - highest precedence! *)
  141.     | ID LPAREN fun_args RPAREN     () (*function call : see fun_args definition above*)
  142.  
  143. (*arithmetic*)
  144.     | exp PLUS exp          ()
  145.     | exp MINUS exp         ()
  146.     | exp DIVIDE exp            ()
  147.     | exp TIMES exp         ()
  148.  
  149. (*comparison*)
  150.     | exp EQ exp            ()
  151.     | exp NEQ exp           ()
  152.     | exp GT exp            ()
  153.     | exp LT exp            ()
  154.     | exp LE exp            ()
  155.     | exp GE exp            ()
  156.      
  157. (*boolean operators*)
  158.     | exp AND exp           ()
  159.     | exp OR exp            ()
  160.      
  161.  
  162.     | ID LBRACE recs RBRACE     () (*record creation*)
  163.     | ID LBRACK exp RBRACK OF exp   () (*array creation*)
  164.     | lvalue ASSIGN exp         () (*assignment*)
  165.     | IF exp THEN exp ELSE exp      () (*if-then-else*)
  166.     | IF exp THEN exp %prec NO_ELSE     () (*if-then*)
  167.     | WHILE exp DO exp          () (*while*)
  168.     | FOR ID ASSIGN exp TO exp DO exp   ()(*for*)
  169.     | BREAK                 () (*break*)
  170.     | LET decs IN exp_sequence END  () (*let-in-end*)
  171.     | LPAREN exp RPAREN         () (*paren*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement