西安电子科技大学 软件工程 编译原理大作业 使用LR(1)文法完成的编译器
- LR(1).json为LR(1)文法所需的预测分析表(程序读取)
- lexer.py为词法分析器
- myParser.py为语法分析器
- output.txt、output1.txt、test1.txt均为矩阵方式储存的预测分析表(debug使用)
- painter为绘图器
- read.py作用为读取预测分析表
通过做上机题加深对编译器构造原理和方法的理解,巩固所学知识。
-
会用正规式设计简单语言的词法;
-
会用产生式设计简单语言的语法;
-
会用递归下降子程序编写语言的解释器。LR(1)编写解释器
-
循环绘图(FOR-DRAW)
-
比例设置(SCALE)
-
角度旋转(ROT)
-
坐标平移(ORIGIN)
-
注释 (-- 或 //)
函数f(t)=t的图形
origin is (100, 300); -- 设置原点的偏移量
rot is 0; -- 设置旋转角度(不旋转)
scale is (1, 1); -- 设置横坐标和纵坐标的比例
for T from 0 to 200 step 1 draw (t, 0);
-- 横坐标的轨迹(纵坐标为0)
for T from 0 to 150 step 1 draw (0, -t);
-- 纵坐标的轨迹(横坐标为0)
for T from 0 to 120 step 1 draw (t, -t);
-- 函数f(t)=t的轨迹
默认值
origin is (0, 0);
rot is 0;
scale is (1, 1);
- B->P
- A->c
- A->t
- A->u(E)
- A->(E)
- C->A@C
- C->A
- E->E+T
- E->E-T
- E->T
- F->ftaEbEcEd(E,E)
- G->+G
- G->-G
- G->C
- O->op(E,E)
- P->S;P
- P->
- R->rpE
- S->qp(E,E)
- S->U
- S->R
- S->F
- S->O
- T->T*G
- T->T/G
- T->G
class nonterminal(Enum):
BEGIN = "BEHIN" # B
PROGRAM = "PROGRAM" # P
STATEMENT = "STATEMENT" # S
ORIGINSTATEMENT = "ORIGINSTATEMENT" # O
SCALESTATEMENT = "SCALESTATEMENT" # U
ROTSTATEMENT = "ROTSTATEMENT" # R
FORSTATEMENT = "FORSTATEMENT" # F
EXPRESSION = "EXPRESSION" # E
TERM = "TERM" # T
FACOR = "FACTOR" # G
COMPONTENT = "COMPONTENT" # C
ATOM = "ATOM" # A
def tra(c):
if c=='B':
return nonterminal.BEGIN
elif c=='P':
return nonterminal.PROGRAM
elif c=="S":
return nonterminal.STATEMENT
elif c=="O":
return nonterminal.ORIGINSTATEMENT
elif c=="U":
return nonterminal.SCALESTATEMENT
elif c=="R":
return nonterminal.ROTSTATEMENT
elif c=="F":
return nonterminal.FORSTATEMENT
elif c=="E":
return nonterminal.EXPRESSION
elif c=="T":
return nonterminal.TERM
elif c=="G":
return nonterminal.FACOR
elif c=="C":
return nonterminal.COMPONTENT
elif c=="A":
return nonterminal.ATOM
elif c=="o":
return TokenType.ORIGIN
elif c=="p":
return TokenType.IS
elif c=="q":
return TokenType.SCALE
elif c=="r":
return TokenType.ROT
elif c=="f":
return TokenType.FOR
elif c=="t":
return TokenType.T
elif c=="a":
return TokenType.FROM
elif c=="b":
return TokenType.TO
elif c=="c":
return TokenType.STEP
elif c=="d":
return TokenType.DRAW
elif c == "u":
return TokenType.FUNC
elif c == "g":
return TokenType.CONST_ID
elif c == ";":
return TokenType.SEMICO
elif c == "(":
return TokenType.L_BRACKET
elif c == ")":
return TokenType.R_BRACKET
elif c == ",":
return TokenType.COMMA
elif c == "+":
return TokenType.PLUS
elif c == "-":
return TokenType.MINUS
elif c == "*":
return TokenType.MUL
elif c == "/":
return TokenType.DIV
elif c == "@":
return TokenType.POWER
elif c == "#":
return TokenType.NONTOKEN