编译原理课程设计_2013
所对应的语法描述图如下所示:
Program
Block
.
8
Block const ident = number
,
;
var
ident
,
;
;
procedure
ident
;
Block
Statement
Statement ident := Expression
call
ident
begin
Statement
end
const m=7, n=85; var x,y,z,q,r;
11
procedure multiply; var a,b; begin a:=x; b:=y; z:=0; while b>0 do begin if odd b then z:=z+a; a:=2*a; b:=b/2; end end; begin x:=m; y:=n; call multiply; end.
ident
number
(
Expression
)
10
PL/0 编译器有两类存贮,目标代码放在一个固定的存贮数组中,而所需数据组织成一 个栈形式存放。 PL/0 的指令集根据 PL/0 语言的要求而设计,它包括以下的指令: 指令 LIT LOD STO CAL INT JMP, JPC OPR 将常数置于栈顶 将变量值置于栈顶 将栈顶的值赋与某变量 用于过程调用的指令 在数据栈中分配存贮空间 用于 if, while 语句的条件或无条件控制转移指令 一组算术或逻辑运算指令 含义
“new” “operator” “private” “protected” “public” “register” “reinterpret_cast” “return” “short” “signed” “sizeof” “static” “static_cast” “struct” “switch” “template” “this” “throw” “true” “try” “typedef” “typeid” “typename” “union” “unsigned” “using” “virtual” “void’ “volatile’
Statement
;
if
Condition
then
Statement
while
Condition
do
Statement
9
Condition odd Expression
Expression = ≠ < > ≤ ≥ Expression
Expression
+ Term - Term - +
Term
Factor * Factor / Factor
6
WCHAR_T WHILE INT REAL STRING CHARACTER
“wchar_t” while 整数,包括十进制、八进制语言编译程序改写 (1) 人数:1 人 (2) 时间:一周 (3) 问题描述:利用 Flex 及 Bison 工具对所提供的 C 语言编写的 PL/0 编译器进行重 写,要求所完成功能与原有程序相同。 (4) PL/0 语言简介 PL/0语言是Pascal语言的一个子集,是Pascal语言的设计者Niklaus Wirth在其专著
“break” “case” “catch” “char” “class” “const” “const_cast” “continue” “default” “delete” “do” “double” “dynamic_cast” “else” “enum” “explicit” “extern” “false” “float” “for” “friend” “goto” “if” “inline” “int” “log” “long” “mutable” “namespace”
(6) 编译器生成代码
12
2 INT 3 LOD 4 STO 5 LOD 6 STO 7 LIT 8 STO 9 LOD 10 LIT 11 OPR 12 JPC 13 LOD 14 OPR 15 JPC 16 LOD 17 LOD 18 OPR 19 STO 20 LIT 21 LOD 22 OPR 23 STO 24 LOD 25 LIT 26 OPR 27 STO 28 JMP 29 OPR
5
NEW OPERATOR PRIVATE PROTECTED PUBLIC REGISTER R_CAST RETURN SHORT SIGNED SIZEOF STATIC S_CAST STRUCT SWITCH TEMPLATE THIS THROW TRUE TRY TYPEDEF TYPEID TYPENAME UNION UNSIGNED USING VIRTUAL VOID VOLATILE
指令的格式由三部分组成: f 其中,含义如下: 操作码 f:8 种操作码; 层次差 l:嵌套深度 np − nq; 多用途 a: f INT LIT LOD STO CAL JMP JPC OPR (5) PL/0 源代码 l ——— ——— 层次差 层次差 层次差 ——— ——— ——— a 常量 常量 数据地址 数据地址 程序地址 程序地址 程序地址 运算类别 l a
0 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0
5 3 3 4 4 0 5 4 0
----------
allocate storage x a y b 0 z b 0 > if b <= 0 then goto 29 b odd if not(odd(b)) goto 20 z a
1
char ch = ‘a’; cout << "hello C++ "; for (int i = 0; i < 10; i++) a = n + ch; return 0; } 这样看起来非常不直观, 我们希望在网页中代码仍然能保持原来的高亮语 法表示。 (4) 输入:C++源代码文件,即后缀为 cpp 的文件。 (5) 输出:网页文件,即后缀为 html 的文件。 (6) 实现功能:将 C++源代码转换为网页文件,在浏览器中打开网页文件时,网页中 显示 C++源代码并以高亮语法表示显示。 (7) 涉及知识点:词法分析。
2
2.
题目:使用工具实现 C/C++语言词法分析器 (1) 人数:1 人 (2) 时间:一周 (3) 输入:C/C++源代码文件,即后缀为 c/cpp 的文件。 (4) 输出:后缀为 tok 的文本性文件。 (5) 实现功能:使用词法分析的自动生成工具 Flex 生成 C/C++语言的词法分析器 (6) 涉及知识点:词法分析,Flex 工具使用。 词法记号 LB LP RB RP PLUS MINUS MULT DIV MOD PLUSA MINUSA MULA DIVA MODA LS RS LSA RSA NOT BITAND BITOR 含义 “{” “(” “}” “)” “+” “-” “*” “/” “%” “+=” “-=” “*=” “/=” “%=” “<<” “>>” “<<=” “>>=” “!” "&" "|"
13
4.
题目:PL/0 语言编译程序扩展 (1) 人数:1~3 人 (2) 扩展后的 PL/0 语言的语法 Program Block Block . [ConstDecl][TypeDecl][VarDecl][FuncDecl] begin Stmt {; Stmt } end
ConstDecl const ConstDef ; {ConstDef ;} ConstDef TypeDecl TypeDef TypeExp VarDecl VarDec Type ident = number ; type TypeDef {TypeDef } ident = TypeExp ; integer | real | Boolean | array ‘[’number .. number‘]’ of TypeExp var VarDec {VarDec } ident {, ident} : Type ; integer | real | Boolean | ident FuncDec { FuncDec } procedure ident [ ( ForParal ) ]; Block ; | function ident [ ( ForParal ) ] : Type ; Block ; ForParal Stmt ident : Type {; ident : Type } IdentRef := Exp | if Exp then Stmt | if Exp then Stmt else Stmt | begin Stmt {; Stmt } end | while Exp do Stmt | exit | | call ident [ ( ActParal ) ] | write ( Exp {, Exp } ) | read (IdentRef {, IdentRef } ) IdentRef ActParal Exp RelOp ident [ ‘[’Exp‘]’ { ‘[’Exp‘]’ } ] Exp {, Exp } SimpExp RelOp SimpExp | SimpExp = | <> | < | > | <= | >= [+ | ] Term {+ Term | Term | or Term} Factor { Factor | / Factor | div Factor | mod Factor | and Factor} IdentRef | number | ( Exp ) | not Factor | ident [ ( ActParal ) ] | odd (SimpExp) | true | false