201506 CSE340 Lecture 13
-
Upload
javier-gonzalez-sanchez -
Category
Software
-
view
452 -
download
1
Transcript of 201506 CSE340 Lecture 13
CSE340 - Principles of Programming Languages Lecture 13:
Parsing Techniques I Javier Gonzalez-Sanchez [email protected] BYENG M1-38 Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2
1 • Understand de provided source code
(3 rules)
2 • Program the rules PROGRAM, BODY, EXPRESSION, X, Y, R, E, C
(11 rules)
3 • Program the full set of rules in the grammar
(16 rules)
4 • Report syntactical errors (one error and stop)
PANIC MODE
5 • Implement error synchronization
ERROR RECOVERY
Assignment 2
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3
Programming Assignment 2
Level 4
Handling Syntactical Errors (part 1): Error messages
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4
Assignment 2 | Grammar
<PROGRAM> à '{' <BODY> '}’ <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> à <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> ')'
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5
Error Synchronization
PROGRAM
BODY
ASSIGNMENT
VARIABLE
WHILE
IF
RETURN
C
EXPRESSION
X
Y
R
E
A
B
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6
Assignment 2
{}
Input:
Build successful
Output:
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7
Assignment 2
{
hello word }
Input:
Line 2: expected =
Line 2: expected ;
Output:
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8
Assignment 2
{
int x int
int int x;
}
Input:
Line 2: expected ;
Line 3: expected identifier Line 3: expected ;
Line 4: expected identifier
Output:
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9
Assignment 2
{ 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 { } } }
Input:
Line 3: expected value, identifier or (
Output:
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10
Parser | Error Points
PROGRAM
Line N: expected {
Line N: expected }
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11
Parser | Error Points
Line N: expected ;
BODY
Line N: expected identifier or keyword
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12
Parser | Error Points
ASSIGNMENT
Line N: expected =
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13
Parser | Error Points
VARIABLE
Line N: expected identifier
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14
Parser | Error Points
WHILE
Line N: expected (
Line N: expected )
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15
Parser | Error Points
IF
Line N: expected ( Line N: expected )
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16
Parser | Error Points
RETURN
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17
Parser | Error Points
Line N: expected ( Line N: expected )
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
Parser | Error Points
EXPRESSION
X
Y
R
E
A
B
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19
Parser | Error Points C
Line N: expected value, identifier or (
Line N: expected )
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20
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 | Summer 2015 | 21
Updates
1. In Gui.java: make the method writeConsole public
2. In Gui.java: add this as a second parameter for the method Parser.run()
3. In Parser.java: add the attribute gui (line 15) add a second parameter to the method run (line 17) initialize gui (line 18)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22
Homework
Programming Assignment #2
CSE340 - Principles of Programming Languages
Javier Gonzalez-Sanchez
[email protected] Summer 2015
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.