Guest User

Untitled

a guest
Aug 31st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.58 KB | None | 0 0
  1. %{
  2.    open System
  3.    open System.Collections.Generic
  4.    open TreeGen
  5.  
  6.    let globalScope = new Scope()
  7.  
  8.    let mutable scope = globalScope
  9.  
  10.    let enterScope (newScope:Function) =
  11.         newScope.EmitEntry (Console.Out)
  12.         scope <- newScope :> Scope
  13.        
  14.  
  15.    let leaveScope () =
  16.         (scope :?> Function).EmitExit (Console.Out)
  17.         scope <- globalScope
  18.  
  19. %}
  20.  
  21. // The start token becomes a parser function in the compiled code:
  22. %start start
  23.  
  24. // Regular tokens
  25. %token <string> ID
  26.  
  27. // characters
  28. %token LPAREN RPAREN SEMICOLON COLON COMMA DOT LBRACKET RBRACKET
  29.  
  30. // keywords
  31. %token PROGRAM BEGIN END VAR ARRAY OF INTEGER REAL FUNCTION PROCEDURE
  32.  
  33. // operators
  34. %token <string> SIGN MULOP
  35. %token ASSIGNOP
  36.  
  37. // literals
  38. %token <System.Int32> NUM
  39.  
  40. // Misc tokens
  41. %token EOF
  42.  
  43. // This is the type of the data produced by a successful reduction of the 'start'
  44. // symbol:
  45. %type < unit > start
  46.  
  47. %%
  48.  
  49. // These are the rules of the grammar along with the F# code of the
  50. // actions executed as rules are reduced.  
  51. start: Program EOF { () }
  52.  
  53. Program:
  54.     ProgramHeader
  55.     Declarations
  56.     SubprogramDeclarations
  57.     CompoundStatement
  58.     DOT { () } 
  59.  
  60. ProgramHeader:
  61.     PROGRAM ID SEMICOLON { Console.WriteLine("Program: "+ $2) }
  62.  
  63. IdentifierList:
  64.     | ID { [$1] }
  65.     | IdentifierList COMMA ID { $3 :: $1 }
  66.  
  67. Declarations:
  68.  
  69.  
  70.             | { () } // empty rule
  71.             | Declarations VAR ID                            COLON Type SEMICOLON
  72.         Type:                                               { new Variable(scope, $3, $5) |> ignore }
  73.     |                           StandardType            { $1 }                   { new VType.Array 
  74.     | ARRAY LBRACKET NUM DOT             DOT NUM RBRACKET                             OF StandardType                  
  75.     StandardType:                                                             (ElementType = $9, StartIndex = $3, EndIndex = $6) :> VType }
  76.     | INTEGER                                                              { new VType.Integer() :> VType }
  77.      | REAL                                                              { new VType.Real() :> VType }
  78. SubprogramDeclarations:
  79.     |                                                                                   { () }
  80.     | SubprogramDeclarations                                            SubprogramDeclaration SEMICOLON { () }
  81.             SubprogramDeclaration:
  82.                                                                     SubprogramHead
  83.                     Declarations
  84.                                 CompoundStatement { leaveScope () |> ignore }
  85.                                         /* KOCHAM CIĘ! <3 */
  86.                                             SubprogramHead:
  87.                                    
  88.                                    
  89.     | FUNCTION ID Arguments COLON StandardType SEMICOLON { enterScope(new Function(scope, $2, $3, $5)) }
  90.     | PROCEDURE ID Arguments SEMICOLON { enterScope ( new Function(scope, $2, $3))}
  91.  
  92. Arguments:
  93.     | { new ArgumentList() }
  94.     | LPAREN ParameterList RPAREN { $2 }
  95.  
  96. ParameterList:
  97.     | IdentifierList COLON Type { new ArgumentList( new List<string>($1), $3) }
  98.     | ParameterList SEMICOLON IdentifierList COLON Type { $1.Add(new List<string>($3), $5) }
  99.  
  100. CompoundStatement:
  101.     BEGIN
  102.     OptionalStatements
  103.     END { [] }
  104.  
  105. OptionalStatements:
  106.     | { [] } // empty rule
  107.     | StatementList   { [] }
  108.  
  109. StatementList:
  110.     | Statement     { [] }
  111.     | StatementList SEMICOLON Statement { [] }
  112.  
  113.    
  114. Statement:
  115.     | ID ASSIGNOP Expression { new Assignment(scope.LookupLeftHand($1), $3) |> ignore }
  116.     | CompoundStatement { () }
  117.  
  118. Expression:
  119.     SimpleExpression { $1 }
  120.  
  121. SimpleExpression:
  122.     | Term                          { $1 }
  123.     | SIGN Term                     { new SignOperation(scope, $2, $1) :> IExpression }
  124.     | SimpleExpression SIGN Term    { new BinaryOperation (scope, $1, $3, $2) :> IExpression }
  125.  
  126. Term:
  127.     | Factor                        { $1 }
  128.     | Term MULOP Factor             { new BinaryOperation (scope, $1, $3, $2) :> IExpression  }
  129.  
  130. Factor:
  131.     | ID                            { scope.Lookup( $1 )  }
  132.     | NUM                           { new IntLiteral( Value = $1 ) :> IExpression}
  133.     | LPAREN Expression RPAREN      { $2 }
Add Comment
Please, Sign In to add comment