Advertisement
EWTD

Untitled

Feb 27th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. import parser_edsl as pe
  2. import re
  3. from ast_v2 import *
  4. from basic_types import *
  5.  
  6. def make_keyword(image):
  7. return pe.Terminal(image, image, lambda name: None, re_flags=re.IGNORECASE, priority=10)
  8.  
  9. IDENTIFIER = pe.Terminal('IDENTIFIER', '[A-Za-z][A-Za-z0-9_]*', str)
  10. INTEGER = pe.Terminal('INTEGER', '[0-9]+', int)
  11. STRING = pe.Terminal('STRING', '\"[^\"]*\"', str)
  12. FLOAT = pe.Terminal('FLOAT', '[0-9]+\\.([0-9]*)?(e[-+]?[0-9]+)?', float)
  13.  
  14.  
  15. KW_SUB, KW_FUNCTION, KW_END, KW_DIM = map(make_keyword, 'sub function end dim'.split())
  16. KW_EXIT, KW_DECLARE, KW_PRINT = map(make_keyword, "exit declare print".split())
  17. KW_IF, KW_THEN, KW_ELSE, KW_UNTIL, KW_WHILE, KW_DO, KW_FOR, KW_TO, KW_NEXT, KW_LOOP = \
  18. map(make_keyword, 'if then else until while do for to next loop'.split())
  19.  
  20. NProgram, NGlobalSymbols, NGlobalSymbol, NSubroutineProto, NFunctionProto, NSubroutineDef, NFunctionDef = \
  21. map(pe.NonTerminal, "Program GlobalSymbols GlobalSymbol SubroutineProto FunctionProto SubroutineDef FunctionDef".split())
  22.  
  23. NSubroutineDecl, NFunctionDecl, NVariableDecl, NVariableInit, NInitializerList, NInitializerListValues = \
  24. map(pe.NonTerminal, "SubroutineDecl FunctionDecl VariableDecl VariableInit InitializerList InitializerListValues".split())
  25.  
  26. NInitializerListValue, NParametersList, NVarnameOrArrayParam, NCommaList, NVarnameOrArrayArg = \
  27. map(pe.NonTerminal, "InitializerListValue ParametersList VarnameOrArrayParam CommaList VarnameOrArrayArg".split())
  28.  
  29. NArgumentsList, NFuncCallOrArrayIndex, NFuncCall, NVarname, NType, NStatements, NStatement, NAssignStatement = \
  30. map(pe.NonTerminal, "ArgumentsList FuncCallOrArrayIndex FuncCall Varname Type Statements Statement AssignStatement".split())
  31.  
  32. NNonEmptyArgumentsList, NNonEmptyParametersList, NNonEmptyStatements = \
  33. map(pe.NonTerminal, "NonEmptyArgumentsList NonEmptyParametersList NonEmptyStatements".split())
  34.  
  35. NExitStatement, NElseStatement, NLoop, NForLoop, NWhileLoop, NPreOrPostLoop = \
  36. map(pe.NonTerminal, "ExitStatement ElseStatement Loop ForLoop WhileLoop PreOrPostLoop".split())
  37.  
  38. NPreLoop, NPostLoop, NPostLoopExpr = \
  39. map(pe.NonTerminal, "PreLoop PostLoop PostLoopExpr ".split())
  40.  
  41. NExpr, NCmpOp, NArithmExpr, NAddOp, NTerm, NPower, NConst, NMulOp = \
  42. map(pe.NonTerminal, 'Expr CmpOp ArithmExpr AddOp Term Power Const MulOp'.split())
  43.  
  44. NProgram |= NGlobalSymbols, Program.create
  45.  
  46. NGlobalSymbols |= NGlobalSymbol, NGlobalSymbols, lambda vd, vds: [vd] + vds
  47. NGlobalSymbols |= lambda: []
  48. NGlobalSymbol |= NSubroutineDecl
  49. NGlobalSymbol |= NSubroutineDef
  50. NGlobalSymbol |= NFunctionDecl
  51. NGlobalSymbol |= NFunctionDef
  52. NGlobalSymbol |= NVariableDecl
  53.  
  54. NSubroutineProto |= KW_SUB, IDENTIFIER, "(", NParametersList, ")", SubroutineProto.create
  55. NFunctionProto |= KW_FUNCTION, NVarname, "(", NParametersList, ")", FunctionProto.create
  56. NSubroutineDef |= NSubroutineProto, NStatements, KW_END, KW_SUB, SubroutineDef.create
  57. NFunctionDef |= NFunctionProto, NStatements, KW_END, KW_FUNCTION, FunctionDef.create
  58. NSubroutineDecl |= KW_DECLARE, NSubroutineProto, SubroutineDecl.create
  59. NFunctionDecl |= KW_DECLARE, NFunctionProto, FunctionDecl.create
  60. NVariableDecl |= KW_DIM, NVarnameOrArrayArg, NVariableInit, VariableDecl.create
  61.  
  62. NVariableInit |= '=', NExpr
  63. NVariableInit |= '=', NInitializerList
  64. NVariableInit |= lambda: None
  65. NInitializerList |= '{', NInitializerListValues, '}', InitializerList.create
  66. NInitializerList |= '{}', InitializerList.create
  67. NInitializerListValues |= NInitializerListValue, ',', NInitializerListValues, lambda v, vs: [v] + vs
  68. NInitializerListValues |= NInitializerListValue, lambda v: [v]
  69. NInitializerListValue |= NExpr
  70. NInitializerListValue |= NInitializerList
  71.  
  72. NParametersList |= NVarnameOrArrayParam, ",", NNonEmptyParametersList, lambda vd, vds: [vd] + vds
  73. NParametersList |= NVarnameOrArrayParam, lambda vd: [vd]
  74. NParametersList |= lambda: []
  75.  
  76. NNonEmptyParametersList |= NVarnameOrArrayParam, ",", NNonEmptyParametersList, lambda vd, vds: [vd] + vds
  77. NNonEmptyParametersList |= NVarnameOrArrayParam, lambda vd: [vd]
  78.  
  79. NCommaList |= ',', NCommaList, lambda vs: 1 + vs
  80. NCommaList |= lambda: 1
  81.  
  82. NVarnameOrArrayParam |= NVarname, Variable.create
  83. NVarnameOrArrayParam |= NVarname, "(", NCommaList, ")", Array.create
  84.  
  85. NArgumentsList |= NExpr, ",", NNonEmptyArgumentsList, lambda vd, vds: [vd] + vds
  86. NArgumentsList |= NExpr, lambda vd: [vd]
  87. NArgumentsList |= lambda: []
  88.  
  89. NNonEmptyArgumentsList |= NExpr, ",", NNonEmptyArgumentsList, lambda vd, vds: [vd] + vds
  90. NNonEmptyArgumentsList |= NExpr, lambda vd: [vd]
  91.  
  92. NVarnameOrArrayArg |= NVarname, Variable.create
  93. NVarnameOrArrayArg |= NVarname, "(", NNonEmptyArgumentsList, ")", Array.create
  94. NVarnameOrArrayArg |= NVarname, "(", NCommaList, ")", Array.create
  95.  
  96. NFuncCallOrArrayIndex |= NVarname, "(", NArgumentsList, ")", FuncCallOrArrayIndex.create
  97. NFuncCallOrArrayIndex |= IDENTIFIER, "(", NArgumentsList, ")", FuncCall.create
  98.  
  99. NFuncCall |= KW_PRINT, NNonEmptyArgumentsList, FuncCall.create_print
  100.  
  101. NVarname |= IDENTIFIER, NType, Varname.create
  102. NType |= "%", lambda: IntegerT()
  103. NType |= "&", lambda: LongT()
  104. NType |= "!", lambda: FloatT()
  105. NType |= "#", lambda: DoubleT()
  106. NType |= '$', lambda: StringT()
  107.  
  108. NStatements |= NStatement, NNonEmptyStatements, lambda vd, vds: [vd] + vds
  109. NStatements |= NExitStatement, lambda vd: [vd]
  110. NStatements |= NStatement, lambda vd: [vd]
  111. NStatements |= lambda: []
  112.  
  113. NNonEmptyStatements |= NStatement, NNonEmptyStatements, lambda vd, vds: [vd] + vds
  114. NNonEmptyStatements |= NExitStatement, lambda vd: [vd]
  115. NNonEmptyStatements |= NStatement, lambda vd: [vd]
  116.  
  117. NStatement |= NVariableDecl
  118. NStatement |= NAssignStatement
  119. NStatement |= NFuncCallOrArrayIndex, "=", NExpr, AssignStatement.create
  120. NStatement |= NFuncCallOrArrayIndex
  121. NStatement |= NFuncCall
  122. NStatement |= NLoop
  123. NStatement |= KW_IF, NExpr, KW_THEN, NStatements, NElseStatement, KW_END, KW_IF, IfElseStatement.create
  124.  
  125. NAssignStatement |= NVarname, "=", NExpr, AssignStatement.create
  126.  
  127. NExitStatement |= KW_EXIT, KW_FOR, ExitFor.create
  128. NExitStatement |= KW_EXIT, KW_FOR, NVarname, ExitFor.create
  129. NExitStatement |= KW_EXIT, KW_DO, ExitWhile.create
  130. NExitStatement |= KW_EXIT, KW_LOOP, ExitWhile.create
  131. NExitStatement |= KW_EXIT, KW_SUB, ExitSubroutine.create
  132. NExitStatement |= KW_EXIT, KW_FUNCTION, ExitFunction.create
  133.  
  134. NElseStatement |= KW_ELSE, NStatements
  135. NElseStatement |= lambda: []
  136.  
  137. NLoop |= NForLoop
  138. NLoop |= NWhileLoop
  139.  
  140. NForLoop |= KW_FOR, NVarname, "=", NExpr, KW_TO, NExpr, NStatements, KW_NEXT, NVarname, ForLoop.create
  141.  
  142. NWhileLoop |= KW_DO, NPreOrPostLoop
  143.  
  144. NPreOrPostLoop |= KW_WHILE, NPreLoop, lambda expr_and_stmts: WhileLoop(expr_and_stmts[1], expr_and_stmts[0], WhileType.PreWhile)
  145. NPreOrPostLoop |= KW_UNTIL, NPreLoop, lambda expr_and_stmts: WhileLoop(expr_and_stmts[1], expr_and_stmts[0], WhileType.PreUntil)
  146. NPreOrPostLoop |= NPostLoop
  147.  
  148. NPreLoop |= NExpr, NStatements, KW_LOOP, lambda expr, stmts: (expr, stmts)
  149.  
  150. NPostLoop |= NStatements, KW_LOOP, NPostLoopExpr, lambda stmts, type_and_expr: WhileLoop(stmts, type_and_expr[1], type_and_expr[0])
  151.  
  152. NPostLoopExpr |= KW_WHILE, NExpr, lambda expr: (WhileType.PostWhile, expr)
  153.  
  154. NPostLoopExpr |= KW_UNTIL, NExpr, lambda expr: (WhileType.PostUntil, expr)
  155.  
  156. NPostLoopExpr |= lambda: (WhileType.Endless, None)
  157.  
  158. NExpr |= NArithmExpr
  159. NExpr |= NArithmExpr, NCmpOp, NArithmExpr, BinOpExpr.create
  160.  
  161. for op in ('>', '<', '>=', '<=', '=', '<>'):
  162. NCmpOp |= op, lambda: op
  163.  
  164. NArithmExpr |= NTerm
  165. NArithmExpr |= NAddOp, NTerm, UnaryOpExpr.create
  166. NArithmExpr |= NArithmExpr, NAddOp, NTerm, BinOpExpr.create
  167.  
  168. NAddOp |= '+', lambda: '+'
  169. NAddOp |= '-', lambda: '-'
  170.  
  171. NTerm |= NPower
  172. NTerm |= NTerm, NMulOp, NPower, BinOpExpr.create
  173.  
  174. NMulOp |= '*', lambda: '*'
  175. NMulOp |= '/', lambda: '/'
  176.  
  177. NPower |= NVarname, Variable.create
  178. NPower |= NConst
  179. NPower |= '(', NExpr, ')'
  180. NPower |= NFuncCallOrArrayIndex
  181.  
  182. NConst |= INTEGER, ConstExpr.createInt
  183. NConst |= FLOAT, ConstExpr.createFl
  184. NConst |= STRING, ConstExpr.createStr
  185.  
  186. if __name__ == "__main__":
  187. p = pe.Parser(NProgram)
  188. if not p.is_lalr_one():
  189. p.print_table()
  190. assert p.is_lalr_one()
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement