Principles of Programming Languages - ASU 2014
Principles of Programming Languages - ASU 2014
Programming Languages
Lecture 01:
Course Presentation
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Definitions
CSE340 - Principles of
Programming Languages
C++
Java
Fortran C
High-Level Language
Assembly Language
Machine Language
Hardware
X,E,G,O,O
01001010101000010
#e1,I,I,0,7
01010100101010010
@
10100100000011011
OPR 19, AX
11010010110101111
STO x, AX
00010010101010010
LIT 5, AX
10101001010101011
OPR 21, AX
LOD #e1,AX
CAL 1, AX
OPR 0, AX Assembler
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 6
Language Paradigms
Final Comprehensive
20% 20%
Exam
Programming
10% + 10% + 10% + 10% 40%
Assignments (4)
100%
97 A+ 86 B+ 74 C+
93 A 82 B 70 C
89 A- 78 B-
Chapter 1. Introduction
Chapter 6. Syntax
Chapter 7. Basic Semantics
Chapter 8. Data Types
Chapter 9. Expressions and Statements
Chapter 10. Procedures
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 02:
Introduction
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Teaching Assistants
• Bolun Li
[email protected]
Graduate Student / Computer Science MS
• Steven Lombardi
[email protected]
Undergraduate Student / Computer Science BS
• Javier Gonzalez-Sanchez
[email protected]
Graduate Teaching Associate
www.javiergs.com
Input:
Symbols
Lexical
Level Output:
Words
Paradigm Input:
Analysis Words
Syntax
Language
Output:
Translate or Sentences
Execute
Input:
Semantic
Sentences
int x = 5;
float y = "hello;
String @z = "9.5";
int x = cse340;
x = x; for ( ; ; );
y = 13.45.0;
int me = 99999000001111222000000111111222223443483045830948;
while { x != 9} ();
X,E,G,O,O
01001010101000010
#e1,I,I,0,7
01010100101010010
@
10100100000011011
OPR 19, AX
11010010110101111
STO x, AX
00010010101010010
LIT 5, AX
10101001010101011
OPR 21, AX
LOD #e1,AX
CAL 1, AX
OPR 0, AX Assembler
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 6
Keywords
Alphabet Symbol
Token
int x = 5;
float y = "hello;
String @z = "9.5";
int x = cse340;
x = x; for ( ; ; );
y = 13.45.0;
int me = 99999000001111222000000111111222223443483045830948;
while { x != 9} ();
(5 == 5) if (int a) a = 1; x = x;
for ( ; ; );y = 13.45.0;int me
=99999000001111222000000111111222
223443483045830948;while { x !=
9} ();int {x} = 10;
9
int x = 5; float y = "hello;
12
String@z="9.5”;intx=cse340;if(x>
3
14) while
(5 == 5) if (int a) a = 1; x = x; 18
=99999000001111222000000111111222 2
223443483045830948;while { x != 6
3
”hello "world" bye"
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 11
Keywords
Alphabet Symbol
Regular
Expression
Token Rules
Deterministic
Finite
Automata
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 03:
Lexical Analysis
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Keywords
Alphabet Symbol
Regular
Expression
Token Rules
Deterministic
Finite
Automata
floatDraftTwo {[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?}
1. These definitions are NOT fully complete or correct. They purpose is only to exemplify RE. For
instance 07 match as an integer, which will NOT be the case for our language.
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 5
Regular expression | Operators
binary
0b(0|1)+
string
{‘}
{‘} {.} Char
Operator
{+,-,*,/,%, {.}
<,>,=,!,…}
{“} {“}
Start
{(, ), {, }, [, ]}
String
Delimiter {a-z}
{0-9}
{\.} {_}
ID
{$}
{0-9}
Integer
Float
{$, _, 0-9, a-z}
Regular expression
Regular expression
Regular expression
Deterministic
Finite Automa
Regular expression
-9
Is this correct?
Is this correct?
Is this correct?
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 15
Handwritten notes
Error
Correct
foobar5 {"ab", "c"}* {ε, "ab", "c", "abab", "abc", "cab", "cc", "ababab",
"ababc", "abcab”, ...}
a) URLs
b) Email addresses
c) ZIP codes
a) URLs
b) Email addresses
c) ZIP codes
a) Reserved words
c) Identifiers
d) Literals or constants
§ Keywords =
§ Operator =
§ Delimiter =
§ ID =
§ Float =
§ Integer =
§ Hexadecimal =
§ Octal =
§ Binary =
§ String =
§ Char =
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 22
Homework
Share them on Blackboard and discuss their correctness with your classmates.
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 04:
Lexer Implementation 1
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Review
-5
-5.5e-5
5-5
[0-9]+.[0-9]+
[0-9]+\.[0-9]+
Regular
Expresion
Regular
Expresion
February 13th, 2008 by Rich Sharpe. Posted in Software Quality, Software Quality Metrics
mark
S0
SE
S1
SE
SE
Stop
S0 S1 S2 S3
S1
S2
SE
SE
SE
Stop
S2
SE
S3
S3
SE
Stop
S3
SE
S3
S3
SE
Stop
SE
SE
SE
SE
SE
Stop
Share them on Blackboard and discuss their correctness with your classmates.
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 05:
Lexer Implementation 2
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Review
2. The dot:
foobar1 = .
foobar1 = .*
foobar1 = .+
foobar1 = \.
URL definition:
ZIP definition:
[1-9][1-9][1-9][1-9][1-9](-[1-9][1-9][1-9][1-9])?
[0-9]{5}(-[0-9]{4})?
• Only BINARY, DELIMITER, and OPERATOR are implemented. You will implement the rest of the
required tokens (rules).
Programming Assignment #1
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 06:
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Errata #1
?
70%
?
100%
?
100%
?
70%
§ Firstname_Lastname_P1.zip
§ Compile and Run
§ Recognize BINARY approx. 20%
§ Recognize DELIMITER and OPERATOR
§ Recognize INTEGER
§ Recognize OCTAL
§ Recognize HEXADECIMAL approx. 40%
§ Recognize IDENTIFIER
§ Recognize STRING
§ Recognize CHAR
§ Recognize KEYWORD approx. 40%
§ Recognize FLOAT
§ 000000005
§ 000000009
§ 000000009.1
§ 000000005
§ 000000005.1
§ 0x0000002
§ 0123456789
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 10
Review | Lexical Analysis
§ 1.2e---3++
§ $50
§ float ________________ = 5;
§ double x = 000000.1;
§ '''a'
§ '\''b'
§ '\'b'
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 11
Review | Lexical Analysis
§ " \\\\\\\\\\a"
§ "Hello""world"
§ abc"Hello"
§ ''’
§ '\x’
§ ’\a'
§ ’\w’
§ "\\\"
(step by step)
INTEGER
OCTAL
HEXADECIMAL
IDENTIFIER
columns
[2-7]
[8-9]
[A]
B
[C-F]
[G-W]
X
[Y-Z]
5 = OCTAL
7
8
=
=
INTEGER
IDENTIFIER
[a-f] = [A] B [C-F]
9 = BINARY
10 = HEXADECIMAL
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 17
Lexer – Step by Step
0 $ _ [1] [2-7] [8-9] A B [C-F] [G-W] X [Y-Z] ... Delimiter, operator,
whitespace, quotation
mark
S0
S1
S3
S3
S2
S2
S2
S3
S3
S3
S3
S3
S3
SE
Stop
S1
S5
SE
SE
S5
S5
SE
SE
S4
SE
SE
S6
SE
SE
Stop
S2
S7
SE
SE
S7
S7
S7
SE
SE
SE
SE
SE
SE
SE
Stop
S3
S8
S8
S8
S8
S8
S8
S8
S8
S8
S8
S8
S8
SE
Stop
S4
S9
SE
SE
S9
SE
SE
SE
SE
SE
SE
SE
SE
SE
Stop
S5
S5
SE
SE
S5
S5
SE
SE
SE
SE
SE
SE
SE
SE
Stop
S6
S10
SE
SE
S10
S10
S10
S10
S10
S10
SE
SE
SE
SE
Stop
S7
S7
SE
SE
S7
S7
S7
SE
SE
SE
SE
SE
SE
SE
Stop
S8
S8
S8
S8
S8
S8
S8
S8
S8
S8
S8
S8
S8
SE
Stop
S9
S9
SE
SE
S9
SE
SE
SE
SE
SE
SE
SE
SE
SE
Stop
S10
S10
SE
SE
S10
S10
S10
S10
S10
S10
SE
SE
SE
SE
Stop
SE
SE
SE
SE
SE
SE
SE
SE
SE
SE
SE
SE
SE
SE
Stop
Review Recursion
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 07:
Syntactic Analysis 1
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Next Step
X,E,G,O,O
01001010101000010
#e1,I,I,0,7
01010100101010010
@
10100100000011011
OPR 19, AX
11010010110101111
STO x, AX
00010010101010010
LIT 5, AX
10101001010101011
OPR 21, AX
LOD #e1,AX
CAL 1, AX
OPR 0, AX Assembler
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 6
Outline
Symbols
Rules
Token
Lexical Analysis
(Lexer)
Regular Expression
Tools
DFA
Language
Terminal
Grammar
(Rules)
Non-terminal
Syntactic
Analysis
BNF
(Parser)
(Backus-Naur Form)
Tools
Syntax Diagrams
E à E OP E
E à integer
OP à + | - | * | /
E à ( E )
A Grammar is a collection of
four elements:
E à E OP E
§ Set of nonterminal symbols
(uppercase)
E à integer
§ Set of terminal symbols
(lowercase). Terminals can be
OP à + | - | * | / tokens or specific words
§ A start symbol
E E à E OP E
⇒ E OP E
⇒ integer OP E E à integer
⇒ integer / E
⇒ integer / integer OP à + | - | * | /
E à ( E )
E E à E OP E
⇒ E OP E
⇒ integer OP E E à integer
⇒ integer * E
⇒ integer * (E) OP à + | - | * | /
⇒ integer * (E OP E)
⇒ integer * (integer OP E) E à ( E )
⇒ integer * (integer + E)
⇒ integer * (integer + integer)
E E à E OP E
⇒ E OP E
⇒ E OP (E) E à integer
⇒ E OP (E OP E)
⇒ E OP (E OP integer) OP à + | - | * | /
⇒ E OP (E + integer)
⇒ E OP (integer + integer) E à ( E )
⇒ E * (integer + integer)
⇒ integer * (integer + integer)
H2 O Notation 1:
S O3 Notation 2:
C O 2
⇒ Elem Comp
Elem → H|O|C|S|Na|Cl| ...
⇒ C Comp
⇒ C Term Num Num → 1|2|3|4| …
⇒ C Elem Num
⇒ CO Num
⇒ CO2
STMT → EXPR; |
if (EXPR) BLOCK |
while (EXPR) BLOCK |
BLOCK |
. . .
Using the rules in the previous slide, apply derivation to show that the following
expression is syntactically correct
while ( 5 ) { if ( 6 ) { } }
Review Recursion
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 08:
Syntactic Analysis II
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Outline
Non-terminal
Grammar
(Rules)
Terminal
Syntactic Parse
Language Analysis Derivation
Tree
(Parser)
BNF
Tools
Syntax Diagrams
E E à E OP E
⇒ E OP E
⇒ integer OP E E à integer
⇒ integer * E
⇒ integer * (E) OP à + | - | * | /
⇒ integer * (E OP E)
⇒ integer * (integer OP E) E à ( E )
⇒ integer * (integer + E)
⇒ integer * (integer + integer)
E E à E OP E
⇒ E OP E
⇒ E OP (E) E à integer
⇒ E OP (E OP E)
⇒ E OP (E OP integer) OP à + | - | * | /
⇒ E OP (E + integer)
⇒ E OP (integer + integer) E à ( E )
⇒ E * (integer + integer)
⇒ integer * (integer + integer)
E à E OP E
E à integer
OP à + | - | * | /
E à ( E )
Syntax Diagrams
à or ::= is defined as
| or operator
<nonterminal> or use uppercases
terminal (lowercases)
* Niklaus Wirth
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 14
Example
§ draw(x, y, z);
§ done();
§ sqrt(x);
BNF
<call_method> à identifier(<identifiers>); | identifier();
<identifiers> à identifier | identifier,<identifiers>
EBNF
<call_method> à identifier ('('<identifiers>')' | '('')' ) ';'
<identifiers> à identifier | identifier,<identifiers>
EBNF
<call_method> à identifier'('[<identifiers>]')' ';'
<identifiers> à identifier | identifier,<identifiers>
EBNF
<call_method> à identifier'('[<identifiers>]')' ';'
<identifiers> à identifier { ,identifier }
Call_method
Identifiers
STMT → EXPR; |
if (EXPR) BLOCK |
while (EXPR) BLOCK |
BLOCK |
. . .
BLOCK STMTS
STMT EXPR
STMT → EXPR; |
if '(' EXPR ')' BLOCK |
while '(' EXPR ')' BLOCK |
BLOCK |
. . .
BLOCK
STMT EXPR
while ( 5 ) { if ( 6 ) { } }
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 09:
Grammars 1
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Ambiguity
• Recursive constructions
Root
Start symbol
Rule 1
Rule 2
Rule 3
Layers
Rule 4
Rule 5
.....
inputs:
§ 1+2+3<4*5
§ 1*2+3+4<5
§ 1<2+3+4*5
§ 1+2<3*4+5
§ 1+2*3<4+5
1
2
3
4
5
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 6
Exercise
10 * 20 + 15
þ (10 * 20) + 15
ý 10 * (20 + 15)
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 7
Exercise | Precedence
<C> à integer
<C> à integer
E B
A C
10 * 20 + 15
(10 * 20) + 15
10 * (20 + 15)
(10) * (20) + (15)
(10 * 20 + 15)
10 * (20) + 15
E B
A C
A * 20 + time
(x * y) + 15
10 * (ASU + cse340)
(10) * (20) + (15)
(hello * world + Arizona)
10 * (counter) + 15
E B
A C
Not ambiguous
Precedence of operators
|
&
!
< > == != <= >=
+-
*/
-
()
...
{ }
{ }
{ } { } { } { }...
{ } { }
{ } { }
EXPRESSION
Y R
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 10:
Grammars II
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours by appointment
Exam 1 | Review
Note:
a*
a+ = a a*
ACM,
1303-1312.
Terminals Non-terminals
<AUTORS>
string <YEAR>
number <TITLE>
<CONFERENCE>
. <PUBLISHER>
, <ADDRRESS>
<PAGES>
-
Rules
<REFERENCE> à
<AUTORS> à
<YEAR> à
<TITLE> à
<CONFERENCE> à
<PUBLISHER> à
<ADDRRESS> à
<PAGES> à
Syntax diagram
Parse tree
(it is incomplete)
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 11:
Parser Implementation I
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Parser
Grammar
AA
BNF B
EBNF
Parser
<BODY> à {<EXPRESSION>';'}
<C> à integer|
identifier|'(' <EXPRESSION> ')'
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 4
Parser | Input and Output
0;
1 + 2;
3 * (4 + hello);
}
Parser
PROGRAM
if (tokens.get(currentToken).getWord().equals(“{”)) {
currentToken++;
else
error(1);
RULE_BODY();
if (tokens.get(currentToken).getWord().equals(“}”))
currentToken++;
else
error(2);
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 8
Parser
BODY
while (!tokens.get(currentToken).getWord().equals(“}”)) {
RULE_EXPRESSION();
if (tokens.get(currentToken).getWord().equals(“;”))
currentToken++;
else
error(3);
}
}
EXPRESSION
RULE_X();
while (tokens.get(currentToken).getWord().equals(“|”)) {
currentToken++;
RULE_X();
}
RULE_Y();
while (tokens.get(currentToken).getWord().equals(“&”)) {
currentToken++;
RULE_Y();
}
if (tokens.get(currentToken).getWord().equals(“!”)) {
currentToken++;
}
RULE_R();
R
public static void RULE_R() {
RULE_E();
while ( tokens.get(currentToken).getWord().equals(“<”)
|tokens.get(currentToken).getWord().equals(“>”)
|tokens.get(currentToken).getWord().equals(“==”)
|tokens.get(currentToken).getWord().equals(“!=”)
) {
currentToken++;
RULE_E();
}
RULE_A();
while (tokens.get(currentToken).getWord().equals(“-”)
| tokens.get(currentToken).getWord().equals(“+”)
) {
currentToken++;
RULE_A();
}
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 14
Parser
RULE_B();
while (tokens.get(currentToken).getWord().equals(“/”)
| tokens.get(currentToken).getWord().equals(“*”)
) {
currentToken++;
RULE_B();
}
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 15
Parser
if (tokens.get(currentToken).getWord().equals(“-”)) {
currentToken++;
}
RULE_C();
Programming Assignment 2
Level 1
Programming Assignment 2
Level 2
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 12:
Parser Implementation II
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-21
Office Hours: By appointment
Review
Programming Assignment 2
Level 1
Programming Assignment 2
Level 2
<BODY> à {<EXPRESSION>';'}
<C> à integer|
identifier|'(' <EXPRESSION> ')'
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 5
Programming Assignment 2
Level 3
Control
Structures KEYWORD { if, else, while, switch, case }
Delimiter : ; , () {} []
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 7
Assignment 2 | Grammar
<BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'}
<ASSIGNMENT> à identifier '=' <EXPRESSION>
<VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier
<WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM>
<IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>]
<RETURN> à 'return'
<PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’
{
float a;
x = 0;
int x;
y = 1 + 1;
x = (0b11) +(05 – 0xFF34);
while (2 == "hi") {
a = 2 > (4 + Y);
if (true) { if( 2 + 2 ) {} else {} }
}
print ("hello" + "world");
}
{
int x;
x = 5;
x = 05;
x = 0x5ff;
x = 5.55;
x = "five";
x = ’5';
x = false;
}
{
int x;
float x;
string x;
char x;
void x;
boolean x;
}
{
x = "hello" + "world" – 'w' * 5 / 3.4;
x = y – hello & 0xffff | 05;
x = -7;
x = !y;
x = (cse340 + cse310) / cse101 ;
}
------------ program(root);
PROGRAM
if (tokens.get(currentToken).getWord().equals(“{”)) {
currentToken++;
else
error(1);
RULE_BODY();
if (tokens.get(currentToken).getWord().equals(“}”))
currentToken++;
else
error(2);
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 14
Assignment 2 | Code
public static void RULE_BODY() {
BODY
while (!tokens.get(currentToken).getWord().equals(“}”)) {
if (tokens.get(currentToken).getToken().equals(“identifier”)) {
RULE_ASSIGNMENT();
if (tokens.get(currentToken).getWord().equals(“;”)) {
currentToken++;
else error(3);
} else if (tokens.get(currentToken).getToken().equals(“int”) | ...) {
RULE_VARIABLE();
if (tokens.get(currentToken).getWord().equals(";")) {
currentToken++;
else error(3);
} else if (tokens.get(currentToken).getWord().equals(“while”)) {
RULE_WHILE();
} else if (tokens.get(currentToken).getWord().equals(“if”)) {
RULE_IF();
} else if (tokens.get(currentToken).getWord().equals(“return”)) {
RULE_RETURN();
if (tokens.get(currentToken).getWord().equals(“;”)) {
currentToken++;
else
error(3);
} else error(4);
}
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 15
Assignment 2 | Code
ASSIGNMENT
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 16
Assignment 2 | Code
VARIABLE public static void RULE_VARIABLE() {
WHILE
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 18
Assignment 2 | Code
IF
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 19
Assignment 2 | Code
RETURN
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 20
Assignment 2 | Code
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 21
Assignment 2 | Code
C public static void RULE_C() {
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 22
PREDICTIVE
DESCENDENT
RECURSIVE
PARSER
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 23
Concepts
{
int a;
a = 0xFF + 0b111; PREDICTIVE
while (a != 05) {
if (true) { DESCENDENT
a = 2.5e-1 / 7;
} else {
a = 'A’;
while(true) {
RECURSIVE
}
}
}
}
print ("hello"); PARSER
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 24
Homework
Programming Assignment #2
(Complete Levels 1 to 3)
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 13:
Parsing Techniques I
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-38
Office Hours: By appointment
Assignment 2
<BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'}
<ASSIGNMENT> à identifier '=' <EXPRESSION>
<VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier
<WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM>
<IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>]
<RETURN> à 'return'
<PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’
EXPRESSION
B
X
C
Y
ASSIGNMENT
VARIABLE
R
WHILE E
IF
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 5
Assignment 2
Input:
{}
Output:
Build successful
Input:
{
hello word
}
Output:
Line 2: expected =
Line 2: expected ;
Input:
{
int x
int
int int x;
}
Output:
Line 2: expected ;
Line 3: expected identifier
Line 3: expected ;
Line 4: expected identifier
Input:
{
x = a;
x = 0x36AW;
x = ((((((((((y))))))))));
x = (5+(4-(3+(5+5/(2+(3+(1+(77+(1-(y)))))))))) + “hello” + ‘q’;
if (a < b) {} else {}
if (a < b) {
if (a < b) {
} else {
}
}
}
Output:
Line 3: expected value, identifier or (
Line N: expected {
PROGRAM
Line N: expected }
Line N: expected ;
BODY
ASSIGNMENT
Line N: expected =
VARIABLE
Line N: expected )
WHILE
Line N: expected (
IF
RETURN
EXPRESSION
Line N: expected )
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 19
Assignment 2
public static void error(int err) {
int n = tokens.get(currentToken).getLine();
switch (err) {
case 1: gui.writeConsole("Line” + n + ": expected {”); break;
case 2: gui.writeConsole("Line” + n + ": expected }”); break;
case 3: gui.writeConsole("Line” + n + ": expected ;”); break;
case 4:
gui.writeConsole("Line” +n+": expected identifier or keyword”);
break;
case 5:
gui.writeConsole("Line” +n+": expected =”); break;
case 6:
gui.writeConsole("Line” +n+": expected identifier”); break;
case 7:
gui.writeConsole("Line” +n+": expected )”); break;
case 8:
gui.writeConsole("Line” +n+": expected (”); break;
case 9:
gui.writeConsole("Line” +n+": expected value, identifier, (”);
break;
}
} Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 20
Updates
1. In Gui.java: make the method writeConsole public
3. In Parser.java:
add the attribute gui (line 15)
add a second parameter to the method run (line 17)
initialize gui (line 18)
Input:
{ We will not allow multi-
line expressions; line 3
x = a; and 4 should not be
x = 1 + ( considered as follow:
x = (y;)
if (a < b + ) {} else {} x= 1 + ( x = ( y;)
if (a < b) {
if (a < b) {
} else {
}
}
EXPRESSION
B
X
C
Y
ASSIGNMENT
VARIABLE
R
WHILE E
IF
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 24
Error Recovery
Output:
Line 3: expected value, identifier, (
Line 3: expected )
Line 3: expected ;
// move to the next line
Line 4: expected )
Line 4: expected identifier or keyword
// infinite loop or end
At this point:
to be continued...
Programming Assignment #2
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 14:
Parsing Techniques II
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-38
Office Hours: By appointment
Error Recovery
Input:
{ We will not allow multi-
line expressions; line 3
x = a; and 4 should not be
x = 1 + ( considered as follow:
x = (y;)
if (a < b + ) {} else {} x= 1 + ( x = ( y;)
if (a < b) {
if (a < b) {
} else {
}
}
EXPRESSION
B
X
C
Y
ASSIGNMENT
VARIABLE
R
WHILE E
IF
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 3
Error Recovery
Output:
Line 3: expected value, identifier, (
Line 3: expected )
Line 3: expected ;
// move to the next line
Line 4: expected )
Line 4: expected identifier or keyword
// infinite loop or end
At this point:
Line N: expected {
currentToken++;
Searching for
FIRST(BODY) or }
PROGRAM
Line N: expected }
Line N: expected ;
BODY
ASSIGNMENT
Line N: expected =
currentToken++;
Searching for
FIRST(EXPRESSION) or FOLLOW(EXPRESSION)
VARIABLE
WHILE
Line N: expected (
currentToken++;
Searching for
FIRST(EXPRESSION) or )
currentToken++; currentToken++;
Searching for Searching for
FIRST(EXPRESSION) or ) FIRST(PROGRAM) or
FOLLOW(PROGRAM)
Line N: expected ( Line N: expected )
IF
RETURN
currentToken++;
Searching for
FIRST(EXPRESSION) or )
EXPRESSION
Line N: expected )
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 15
Parser | Error Recovery
Rule FIRST set FOLLOW set
PROGRAM { EOF
ASSIGNMENT identifier ;
IF if } U FIRST(BODY)
RETURN return ;
EXPRESSION FIRST(X) ), ;
X FIRST(Y) | U FOLLOW(EXPRESSION)
R FIRST(E) FOLLOW(Y)
C integer, octal, hexadecimal, binary, true, false, string, char, float, identifier, ( FOLLOW(B)
If ( tokens.get(currentToken).getLine() <
tokens.get(currentToken+1).getLine() ) {
Programming Assignment #2
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 15:
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-38
Office Hours: By appointment
Parser | Error Recovery
Rule FIRST set FOLLOW set
PROGRAM { EOF
ASSIGNMENT identifier ;
IF if } U FIRST(BODY)
RETURN return ;
EXPRESSION FIRST(X) ), ;
X FIRST(Y) | U FOLLOW(EXPRESSION)
R FIRST(E) FOLLOW(Y)
C integer, octal, hexadecimal, binary, true, false, string, char, float, identifier, ( FOLLOW(B)
Definition
FIRST (a) is the set of tokens that can begin the construction a.
Example
<S> → <A><B><C>
<S> → <F>
<A> → <E><F>d
<A> → a
<B> → a<B>b
<B> → ε
<C> → c<C>
<C> → d
<E> → e<E>
<E> → <F>
<F> → <F>f
<F> → ε
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 8
Calculate the FIRST set
1. FIRST(X) = {X}
if X is a terminal
2. FIRST(ε) = {ε}.
note that this is not covered by the first rule because
ε is not a terminal.
loop
FOLLOW (a) is the set of tokens that can follow the construction a.
Example
5 + 4 + -7 * 12 + 75
5 + 4 + ((-7) * 12) + 75
FOLLOW (a) is the set of tokens that can follow the construction a.
Example
<S> → <A><B><C>
<S> → <F>
<A> → <E><F>d
<A> → a
<B> → a<B>b
<B> → ε
<C> → c<C>
<C> → d
<E> → e<E>
<E> → <F>
<F> → <F>f
<F> → ε
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 20
Calculate the FOLLOW set
S → F S={a,ε,e,f,d}
S
{eof}
A={a, e, f, d}
A → EFd B={a, ε}
A → a A
{a} {a, c, d} C= {c, d}
E={e, ε, f}
B → aBb F={ε,f}
B → ε B
{c, d} {c, d, b}
C → cC
C → d C
{eof}
E → eE
E
{f} {f, d}
E → F
F → Ff F
{eof} {eof, d} {eof, d, f}
F → ε
FOLLOW(E) = {$, )}
FOLLOW(T) = {$, ),+ }
FOLLOW(F) = {$, ),+, * }
Rule 1.
It should always be possible to choose among several
alternatives in a grammar rule.
BODY
Rule 1.1
The FIRST sets of any two choices in one rule must not
have tokens in common in order to implement a single-
symbol look ahead predictive parser.
Rule 2.
For any optional part, no token beginning the optional part
can also come after the optional part.
FIRST(RULE) != FOLLOW(RULE)
BODY PROGRAM
Programming Assignment #2
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 16:
Grammars III
Javier Gonzalez-Sanchez
[email protected]
BYENG M1-38
Office Hours: By appointment
Chomsky Hierarchy
*Noam Chomsky
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 2
Chomsky Hierarchy
x = a + b * 75
Type 1 - Context-sensitive
structure: αXβ → αγβ
where X is a non-terminal, and α,β,γ are any string of terminals and nonterminals, (γ
must be non-empty).
Type 2 - Context-free
structure: X → γ|ε
where X is a nonterminal and γ is any string of terminals and nonterminals (may be
empty). It is discouraged to use only one nonterminal as γ.
Type 3 – Regular
structure: X → αY | α |ε
where X,Y are single nonterminals, and α is a string of terminals;
Type 3 – Regular
structure: X → αY | α |ε
where X,Y are single nonterminals, and α is a string of terminals;
<A> → ε
<A> → a
<A> → <B>a
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 6
Regular Grammar
S → aS → aaS → …
→ a…aS →a…abA →a…abcA
→ a…abccA → …
→ a…abc…c
Type 2 - Context-free
structure: X → γ|ε
<S> → <A><S> | ε
<A> → 0 <A> 1| <A>1 | 0 1
<S> → <NP><VP>
<NP> → the <N>
<VP> → <V><NP>
<V> → sings | eats
<N> → cat | song | canary
Type 1 - Context-sensitive
structure: αXβ → αγβ
<S>→ a<S><B><C>
<S> → a<B><C>
<C><B> → <H><B>
<H><B> → <H><C>
<H><C> → <B><C>
a<B> → a b
b<B> → b b
b<C> → b c
c<C> → c c
*The language generated by this grammar is {anbncn|n ≥ 1} .
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 10
Chomsky Hierarchy
Type 1 - Context-sensitive
structure: αXβ → αγβ
where X is a non-terminal, and α,β,γ are any string of terminals and nonterminals, (γ
must be non-empty).
Type 2 - Context-free
structure: X → γ|ε
where X is a nonterminal and γ is any string of terminals and nonterminals (may be
empty). It is discouraged to use only one nonterminal as γ.
Type 3 – Regular
structure: X → αY | α |ε
where X,Y are single nonterminals, and α is a string of terminals;
G1 { G3 {
<s> à<A> <s>à <B><A><B>
<s> à<A><A><B> <s>à <A><B><A>
<A> àaa <A>à <A><B>
<A>a à<A><B>a <A>à a<A>
<A><B> à<A><B><B> <A>à ab
<B>b à<A><B>b <B>à <B><A>
<B> àb <B>à b
} }
G2 { G4 {
<s> à b<s> <s>à <A><B>
<s> à a<A> <A>à a<A>
<s> à b <A>à a
<A>à a<s> <B>à <B>b
<A>à b<A> <B>à b
<A>à a <A><B>à <B><A>
} }
<E> → e<E>
<E> → <F>
<F> → <F>f
<F> → ε
<A> → ε
Type 1 - Context-sensitive
structure: αXβ → αγβ
where X is a non-terminal, and α,β,γ are any string of terminals and nonterminals, (γ
must be non-empty).
Type 2 - Context-free
structure: X → γ|ε
where X is a nonterminal and γ is any string of terminals and nonterminals (may be
empty). It is discouraged to use only one nonterminal as γ.
Type 3 – Regular
structure: X → αY | α |ε
where X,Y are single nonterminals, and α is a string of terminals;
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 17:
Semantic Analysis I
Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
Semantic Analysis
i.e.,
1. Declaration and Unicity. Review for uniqueness and that the variable
has been declared before its usage.
2. Types. Review that the types of variables match the values assigned
to them.
5. Return type. Review that the value returned by a method match the
type of the method.
int char
j method global
int
m
global
void
method-int-char
function
n int
method-int-char
short
l
method-int-char
void
method
function
int
k global
int
method-int function
void foo1(int a) {
// float a = 1;
float w = a;
}
void foo2(char b) {
int a = c + d;
}
int i = a + b;
}
Level 1
<BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'}
<ASSIGNMENT> à identifier '=' <EXPRESSION>
<VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier
<WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM>
<IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>]
<RETURN> à 'return'
<PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’
void rule_variable( ) {
. . .
if (tokens.get(currentToken1).getType().equals(“identifier”)) {
SemanticAnalizer.CheckVariable(
tokens.get(currentToken-1).getWord(),
tokens.get(currentToken).getWord()
);
currentToken++;
} else {
error (6);
}
. . .
Programming Assignment 3
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 18:
Semantic Analysis II
Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
Semantic Analysis
1. Declaration and Unicity. Review for uniqueness and that the variable
has been declared before its usage.
2. Type Matching. Review that the types of variables match the values
assigned to them.
5. Return type. Review that the value returned by a method match the
type of the method.
int x, y, z;
char p, q, r;
float a, b, c; x =
boolean foo;
void method() {
x = a * c + p;
}
+
a c p
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 4
Type matching | Cube
int
float
char
string
boolean
void
cube
char X X X string X X
boolean X X X string X X
void X X X X X X
cube
float X X X X X X
char X X X X X X
string X X X X X X
boolean X X X X boolean X
void X X X X X X
cube
boolean X X X X X X
void X X X X X X
cube
float OK OK X X X X
char X X OK X X X
string X X X OK X X
boolean X X X X OK X
void X X X X X OK
cube
OPERATOR int
float
char
string
boolean
void
OPERATOR int
float
char
string
boolean
void
- +
int
float
X
X
X
X
int
int
float
X
string
X
X
char X X X string X X
boolean X X X string X X
void X X X X X X
int x, y, z;
char p, q, r;
float a, b, c; x =
boolean foo;
void method() {
x = a * c + p;
}
+
a c p
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 12
Type matching | Example 2
symbol table
int a;
int c (int b) {
return b * 3 * 2 * 1 ;
}
void main () {
a = 1;
boolean a= c(14)/2 > 1;
}
Level 2
<BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'}
<ASSIGNMENT> à identifier '=' <EXPRESSION>
<VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier
<WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM>
<IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>]
<RETURN> à 'return'
<PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’
1.
// except for the open parenthesis
SemanticAnalizer.pushStack(
tokens.get(currentToken).getToken()
);
2.
if (operatorWasUsed)
String x = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, “-” );
SemanticAnalizer.pushStack(result);
}
3.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
3.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
3.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
2.
if (operatorWasUsed)
String x = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, “!” );
SemanticAnalizer.pushStack(result);
}
2.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
2.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
if (!result.equals(“OK”) {
error(2);
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 24
Homework
Programming Assignment 3
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 19:
Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
Semantic Analysis
1. Declaration and Unicity. Review for uniqueness and that the variable
has been declared before its usage.
2. Types. Review that the types of variables match the values assigned
to them.
5. Return type. Review that the value returned by a method match the
type of the method.
Level 3
Reviewing Conditions
<BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'}
<ASSIGNMENT> à identifier '=' <EXPRESSION>
<VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier
<WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM>
<IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>]
<RETURN> à 'return'
<PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’
WHILE
String x = SemanticAnalizer.popStack();
if (!x.equals(“boolean”) {
error(3);
}
IF
String x = SemanticAnalizer.popStack();
if (!x.equals(“boolean”) {
error(3);
}
þ Declaration and Unicity. Review for uniqueness and that the variable
has been declared before its usage.
þ Type Matching. Review that the types of variables match the values
assigned to them.
¨ Return type. Review that the value returned by a method match the
type of the method.
Programming Assignment 3
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 20:
Intermediate Code I
Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
A Compiler in Action
Words
Lexer
Tokens
{
int a;
int b;
int c;
int d;
if (a != 5) {
b = c + d;
}
print (a);
X,E,G,O,O
01001010101000010
#e1,I,I,0,7
01010100101010010
@
10100100000011011
OPR 19, AX
11010010110101111
STO x, AX
00010010101010010
LIT 5, AX
10101001010101011
OPR 21, AX
LOD #e1,AX
CAL 1, AX
OPR 0, AX Assembler
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 6
High-Level Languages
compilation execution
X,E,G,O,O
01001010101000010
#e1,I,I,0,7
01010100101010010
@
10100100000011011
OPR 19, AX
11010010110101111
STO x, AX
00010010101010010
LIT 5, AX
10101001010101011
OPR 21, AX
LOD #e1,AX
CAL 1, AX
OPR 0, AX Assembler
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 7
A Simple Virtual Machine
Memory
CPU
ALU
Register
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 8
A Simple Virtual Machine
Memory
program
Code
sto 0, s
sto 0, d
sto 0, c
sto 0, d
lod s, 0
lit “s”, 0
Symbol
opr 14, 0
jmc #a1, false
lod b, 0
Table
CPU
ALU
Register
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 9
A Simple Virtual Machine
Memory
program
Code
sto 0, s
sto 0, d
sto 0, c
sto 0, d
lod s, 0
lit “s”, 0
Symbol
opr 14, 0
jmc #a1, false
lod b, 0
Table
CPU
PC
ALU
Register
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 10
Instructions
instruction first second
parameter parameter
Examples:
LIT 5, 0
LIT 5.5, 0
LIT 'a', 0
LIT ”hello”, 0
LIT true, 0
Examples:
LOD a, 0
LOD hello, 0
LOD cse340, 0
Examples:
STO a, 0
STO hello, 0
STO cse340, 0
Source Code:
{
int a; int b;
a = 10;
b = a;
Symbol Table:
Type Name Scope Value
int a global 0
int b global 0
Examples:
OPR 1, 0 ; return
OPR 2, 0 ; add
OPR 3, 0 ; subtract
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 21:
Intermediate Code II
Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
Review
8
hello
{ Virtual Machine
int a; (interpreter)
int b;
a = 5;
a, int, global, 0
b = 9; b, int, global, 0
a = a + b / 3; @
print (a); LIT 5, 0
STO a, 0
print ("hello"); LIT 9, 0
} STO b, 0
LOD a, 0
LOD b, 0
Lexer
LIT 3, 0
OPR 4, 0
Parser OPR 2, 0
STO a, 0
LOD a, 0
OPR 21,0
Semantic Analyzer Code Generation LOD "hello", 0
OPR 21,0
OPR 0, 0
{ Lexer
int a;
int b; Parser
boolean foo;
a = a / 2;
print ("total:" + a);
return;
}
100
true
total: 50
Virtual Machine
(interpreter)
Programming Lexer
workshops
Parser
Do not required.
Deadline: December 4
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 6
Assignment #4 | Grammar
<PROGRAM> à '{' <BODY> '}’
<BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|
<WHILE>|<IF>|<SWITCH>|<RETURN>';'}
<ASSIGNMENT> à identifier '=' <EXPRESSION>
<VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier
<WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM>
<IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>]
<RETURN> à 'return'
<PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’
<EXPRESSION> à <X> {'|' <X>}
<X> à <Y> {'&' <Y>}
<Y> à ['!'] <R>
<R> à <E> {('>'|'<'|'=='|'!=') <E>}
<E> à <A> {(’+'|'-’) <A>}
<A> à <B> {('*'|'/') <B>}
<B> à ['-'] <C>
<C> à integer | octal | hexadecimal | binary | true | false |
string | char | float | identifier|'(' <EXPRESSION> ')'
No changes required
• Bonus 10 %
Implement SWITCH statement: (a) parser; and (b)
code generation.
• Bonus 10 %
Integration: (a) graphical user interface including
token table, parse tree, and symbol table; (b)
syntactic errors handling and recovery; and (c)
semantic analysis.
assignment bonus
0–100% 0–20%
* *
1
2
3
4
5
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 16
Code
PROGRAM
BODY
ASSIGNMENT
VARIABLE
WHILE
IF
RETURN
EXPRESSION
Assignment #4
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.
CSE340 - Principles of
Programming Languages
Lecture 22:
Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
Review
Programming Assignment 4
Level 1
OPR 0, 0
PROGRAM
BODY
OPR 21, 0
STO identifier, 0
ASSIGNMENT
VARIABLE
identifier, <type>
OPR 1, 0
RETURN
EXPRESSION
if (twice) {
OPR 8, 0
}
if (twice) {
OPR 9, 0
}
if (operatorUsed) {
OPR 10, 0
}
if (twice) {
OPR <operatorNumber>, 0
}
if (twice) {
OPR <operatorNumber>, 0
}
if (twice) {
OPR <operatorNumber>, 0
}
LIT 0, 0
if (operatorUsed) {
OPR 3, 0
}
LIT <value>, 0
LOD identifier, 0
Programming Assignment 4
Level 2
JMP, JMC
• JMP <line>, 0
Put the value <line> in the program counter;
thus, the next line to be executed will be <line>
Examples:
JMP 1, 0
JMP 14, 0
JMP 75, 0
Examples:
JMC 1, true
JMC 14, false
JMC 75, true
{ int a global 0
int a; int b; int b global 0
@
a = 10;
while (a>1){ LIT 10, 0
print (a); STO a, 0
} LOD a, 0
return; LIT 1, 0
} OPR 11, 0 ; >
JMC #e1, false
LOD a, 0
OPR 20, 0 ; print
JMP #e2, 0
OPR 1, 0
OPR 0, 0
if (a < b) { if (a < b) {
if (a>b) goto label #1
//code1 //code1
} else { } else
goto label#2
label #1
// code2 // code2
} }
label #2
{
int a; int b;
a = 10;
while (a>1) {
if (a != 0) {
print (a);
} else {
print (b);
}
a = a -1;
}
}
Javier Gonzalez-Sanchez | CSE340 | Fall 2014 | 27
Handwritten notes
WHILE
IF
int a, b;
switch (a) {
Assignment #4
Javier Gonzalez-Sanchez
[email protected]
Fall 2014
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.