date: 2024-12-21
title: Compiler-As-3
status: DONE
author:
- AllenYGY
tags:
- Assignment
- Compiler
publish: True
Compiler-As-3
Use the grammar rules and attribute definitions in Lecture 10 Page 19 to construct the parse tree and syntax tree for expression "
Parse Tree:
Syntax Tree:
Use the grammar rules and attribute definitions in “Grammar+Typing.docx” (the grammar is in Lec 12 on pg 20) to analyze the source code
bool flip(bool x) not x; flip (flase)
Parse Tree
Symbol Table
Name | Type |
---|---|
flip | |
The language in Lec 11 on Page 27 has two production rules:
So, it is possible to have valid variable declarations
int *[10]x; int[10]* y;
Please follow the corresponding type checking rules on the same page to give the type of
The type of x is
The type of y is
The language in Lec 11 on Page 27 is too weak. It cannot make assignment from a memory location to a pointer. Thus, we power-up the language by modifying the grammar rules. (New or modified rules are in red color.) Define the correct type checking rules for the new grammar rules. Type checking for old grammar rules remain unchanged.
Let the following grammar rules be the syntax of integer arithmetic.
Remark:
"num"
is an integer constant."+"
and "×"
are integer addition and multiplication respectively.However, this language is not perfect. It may contain many redundant parentheses.
For example, ((2 × 3) + 4)
can be entirely without the parentheses as 2 × 3 + 4
.
(You can try to parse them.) Thus, please design semantics for the grammar rules to remove redundant parentheses. (25 pt)
Hint:
"expression"
, which holds the expression for each substructure.E.expression
has the final outcome — the"expression"
is treated as a string. And the operator "Define the semantics for the grammar rules.
expression
: Stores the simplified expression without redundant parentheses.priority
: Stores the priority of the operator in the expression. Define a function for checking whether parentheses are needed for a given expression.
function checkParen(childExpr, childPriority, parentPriority):
if childPriority < parentPriority then
return "(" + childExpr + ")"
else
return childExpr