自制解释器中遇到的困难

标签: 解释器 编译器 发布于:2020-03-23 11:19:13 编辑于:2022-11-15 12:11:55 浏览量:31243

Token 的类型怎么搞?

文法如何写?

抽象语法树的节点种类?

参考:一个 Pascal 解释器的实现

文法

program : PROGRAM variable SEMI block DOT

block : declarations compound_statement

declarations : (VAR (variable_declaration SEMI)+)? procedure_declaration*

variable_declaration : ID (COMMA ID)* COLON type_spec

procedure_declaration :
     PROCEDURE ID (LPAREN formal_parameter_list RPAREN)? SEMI block SEMI

formal_params_list : formal_parameters
                   | formal_parameters SEMI formal_parameter_list

formal_parameters : ID (COMMA ID)* COLON type_spec

type_spec : INTEGER | REAL

compound_statement : BEGIN statement_list END

statement_list : statement
               | statement SEMI statement_list

statement : compound_statement
          | proccall_statement
          | assignment_statement
          | empty

proccall_statement : ID LPAREN (expr (COMMA expr)*)? RPAREN

assignment_statement : variable ASSIGN expr

empty :

expr : term ((PLUS | MINUS) term)*

term : factor ((MUL | INTEGER_DIV | FLOAT_DIV) factor)*

factor : PLUS factor
       | MINUS factor
       | INTEGER_CONST
       | REAL_CONST
       | LPAREN expr RPAREN
       | variable

variable: ID

Token 类型

# single-character token types
PLUS          = '+'
MINUS         = '-'
MUL           = '*'
FLOAT_DIV     = '/'
LPAREN        = '('
RPAREN        = ')'
SEMI          = ';'
DOT           = '.'
COLON         = ':'
COMMA         = ','
# block of reserved words
PROGRAM       = 'PROGRAM'  # marks the beginning of the block
INTEGER       = 'INTEGER'
REAL          = 'REAL'
INTEGER_DIV   = 'DIV'
VAR           = 'VAR'
PROCEDURE     = 'PROCEDURE'
BEGIN         = 'BEGIN'
END           = 'END'      # marks the end of the block
# misc
ID            = 'ID'
INTEGER_CONST = 'INTEGER_CONST'
REAL_CONST    = 'REAL_CONST'
ASSIGN        = ':='
EOF           = 'EOF'

节点种类:

Block,Program,Compound,NoOp,BinOp,ProcedureDecl,VarDecl,Assign,Var,Num,ProcedureCall。

未经允许,禁止转载,本文源站链接:https://iamazing.cn/