CS153 111017
CS153 111017
Shomit Ghose
History of Computing Speaker Wednesday, Oct. 19, 6:00-7:00 PM Auditorium ENGR 189
List and describe five software engineering techniques we employed to make the code manageable and understandable.
Partitioning
language-dependent front end language-independent middle tier and back end The back end can be either an interpreter or a compiler.
Design patterns
strategy, factory, etc. Code to the interfaces. Closed for modification, open for extension.
What is the purpose of the symbol table stack and how does it achieve its purpose?
Purpose: Implement static scoping
Push a symbol table onto the stack whenever the parser enters a scope. Pop the symbol table off the stack when the parser leaves a scope.
Search only the local (topmost) symbol table to determine if an identifier has been declared in the local scope. Search the entire stack from top to bottom to determine if an identifier has been declared in the local or an outer scope.
4. The topmost activation record at level n contains the current values of the local variables and formal parameters of the currently active procedure or function at level n. 5. Use a runtime display to optimize accessing the appropriate activation record on the stack.
Implement the ternary conditional operator in Pascal using the keywords IF, THEN, and ELSE.
a.
The result at run time of evaluating the conditional operator is a single value, the result of evaluating either <expression-2> or <expression-3>. Therefore, a conditional expression must be a factor.
What type checking operations are necessary while parsing a conditional operator?
<expression-1> must be boolean <expression-2> and <expression-3> must be type compatible with the surrounding operators (preferably they should be the same type) or be assignment compatible with the target variable. _
k := i j*IF m-n = 0 THEN m*n ELSE m+n Note that the conditional does not change any precedence rules.
* j IF
k i
= m n 0 m
* n m
+ n
Describe the purpose of each of the following hash tables (or tree maps) and describe its keys (or give an example of a key).
a.
symbol table
Store the symbol table entries for the identifiers declared within given scope Keys: Names of the identifiers
b.
Store the attributes of an identifier Keys: Attribute enum constants such as ROUTINE_CODE
c.
Store attributes about a data type Keys: Attribute enum constants such as ARRAY_INDEX_TYPE
CS 153: Concepts of Compiler Design R. Mak
Store the attributes of a parse tree node Keys: Attribute enum constants LINE, ID, and VALUE
a.
memory map
Store the runtime values of the local variables and formal parameters of a program, procedure, or function Keys: The names of the variables and parameters _
10
Middle tier
No changes
Back end
No changes
SJSU Dept. of Computer Science Fall 2011: October 17 CS 153: Concepts of Compiler Design R. Mak
11
After parsing the record variable following the WITH keyword, the parser must
Determine the record type of the variable. Push the record types symbol table onto the symbol table stack. When parsing the nested statements of the WITH statement, look up identifiers first in the record types symbol table to determine whether or not they are record fields. At the end of the WITH statement, pop off the record types symbol table. _
12
What advantages would a WITH statement have at run time? None at all, if the WITH statement is considered to be shorthand for the programmer (syntactic sugar). However, if the parse tree contains a WITH node, then the record variable only needs to be evaluated once. This would be a performance optimization especially if the record variable is complicated, such as having subscripts, fields, and pointer dereferencing.
c.
How would you implement a WITH statement in the interpreters back end? In the syntactic sugar case, do nothing. In the WITH node case, the interpreter must allocate an extra slot in the activation record to store the value of the record variable.
13
14
Combines Pascal and SQL for writing database applications Not PL/SQL use the language to write client programs Compiled code makes JDBC calls hidden from the programmer
A string-processing language
Combines Pascal and Perl for writing applications that involve pattern matching and string transformations
CS 153: Concepts of Compiler Design R. Mak
15
Separate scanner classes for each token type. Create lots of objects and make lots of method calls.
16
Regular expression: <letter> ( <letter> | <digit> )* Implement the regular expression with a finite automaton (AKA finite state machine):
start state 1 letter transition digit letter 2 accepting state [other] 3
At each state, the next input character uniquely determines which transition to take to the next state.
CS 153: Concepts of Compiler Design R. Mak
17
State-Transition Matrix
letter 1 letter 2 digit [other] 3
18
+ -
3
digit
digit
.
[other]
digit
10
11
[other]
12
[other]
19
letter
1
digit
[other]
/* letter 1, /* 0 */ { /* 1 */ { 1, /* 2 */ { ERR, /* 3 */ { ERR, /* 4 */ { -5, /* 5 */ { ERR, /* 6 */ { ERR, /* 7 */ { -8, /* 8 */ { ERR, /* 9 */ { ERR, /* 10 */ { ERR, /* 11 */ { -12, /* 12 */ { ERR,
+ 3, -2, ERR, ERR, -5, ERR, ERR, -8, ERR, 10, ERR, -12, ERR,
3, -2, ERR, ERR, -5, ERR, ERR, -8, ERR, 10, ERR, -12, ERR,
. ERR, -2, ERR, ERR, 6, ERR, ERR, -8, ERR, ERR, ERR, -12, ERR,
E other */ 1, ERR }, 1, -2 }, ERR, ERR }, ERR, ERR }, 9, -5 }, ERR, ERR }, ERR, ERR }, 9, -8 }, ERR, ERR }, ERR, ERR }, ERR, ERR }, -12, -12 }, ERR, ERR },
digit digit
3
digit
.
[other]
digit
10
11
[other]
12
[other]
5
SJSU Dept. of Computer Science Fall 2011: October 17
8
CS 153: Concepts of Compiler Design R. Mak
= = = = = = =
0; 1; 2; 3; 4; 5; 6; // error state
private static final int matrix[][] = { ... }; private char ch; private int state; ... }
SJSU Dept. of Computer Science Fall 2011: October 17 CS 153: Concepts of Compiler Design R. Mak
21
22
if ((state >= 0) || (state == ERR)) { buffer.append(ch); // build token string nextChar(); } } Table-driven scanners can be very fast! return buffer.toString(); }
SJSU Dept. of Computer Science Fall 2011: October 17 CS 153: Concepts of Compiler Design R. Mak
23
24
Named after John Backus and Peter Naur. Text-based means it can be read by a program ...
such as a compiler-compiler that can automatically generate a parser for a source language after reading (and parsing) the languages syntax rules written in BNF.
Symbols that are part of BNF itself but are not necessarily part of the syntax of the source language.
::= | < > is defined as or Surround names of nonterminal (not literal) items
25
26
An expression is a simple expression optionally followed by an relational operator and another simple expression.
<expression> ::= <simple expression> | <simple expression> <rel op> <simple expression>
27
<digit sequence> ::= <digit> | <digit> <digit sequence> Repetition via recursion. <unsigned integer> ::= <digit sequence> <unsigned real> ::= <unsigned integer>.<digit sequence> | <unsigned integer>.<digit sequence> <e> <scale factor> | <unsigned integer > <e> <scale factor> <unsigned number> ::= <unsigned integer> | <unsigned real> <scale factor> ::= <unsigned integer> | <sign> <unsigned integer> <e> ::= E | e The sign is optional. <sign> ::= + | SJSU Dept. of Computer Science Fall 2011: October 17 CS 153: Concepts of Compiler Design R. Mak
28
<if statement> ::= IF <expression> THEN <statement> | IF <expression> THEN <statement> ELSE <statement>
It should be straightforward to write a parsing method from either the syntax diagram or the BNF. _
29