SlideShare a Scribd company logo
CSE340 - Principles of
Programming Languages
Lecture 18:
Semantic Analysis II
Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2
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.
3.  Array’s indexes. Review that the indexes are integers.
4.  Conditions. Review that all expressions on the conditons return a
boolean value.
5.  Return type. Review that the value returned by a method match the
type of the method.
6.  Parameters. Review that the parameters in a method match in type
and number with the declaration of the method.
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3
2. Type Matching
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4
Type matching | Example 1
int x, y, z;
char p, q, r;
float a, b, c;
boolean foo;
void method() {
x = a * c + p;
}
x
*
+
a c p
=
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5
Type matching | Cube
fill one sheet for
each operator in
the language
cube
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6
Type matching | Cube
OPERATOR int	
 float	
 char	
 string	
 boolean	
 void	
int	
float	
char	
string	
boolean	
void	
cube
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7
Type matching | Cube
OPERATOR
+
int	
 float	
 char	
 string	
 boolean	
 void	
int	
 int	
 float	
 X	
 string	
 X	
 X	
float	
 float	
 float	
 X	
 string	
 X	
 X	
char	
 X	
 X	
 X	
 string	
 X	
 X	
string	
 string	
 string	
 string	
 string	
 string	
 X	
boolean	
 X	
 X	
 X	
 string	
 X	
 X	
void	
 X	
 X	
 X	
 X	
 X	
 X	
cube
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8
Type matching | Cube
OPERATOR
&
int	
 float	
 char	
 string	
 boolean	
 void	
int	
 X	
 X	
 X	
 X	
 X	
 X	
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
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9
Type matching | Cube
OPERATOR
<
int	
 float	
 char	
 string	
 boolean	
 void	
int	
 boolean	
 boolean	
 X	
 X	
 X	
 X	
float	
 boolean	
	
boolean	
	
X	
 X	
 X	
 X	
char	
 X	
	
X	
	
X	
 X	
 X	
 X	
string	
 X	
 X	
 X	
 X	
 X	
 X	
boolean	
 X	
 X	
 X	
 X	
 X	
 X	
void	
 X	
 X	
 X	
 X	
 X	
 X	
cube
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10
Type matching | Cube
OPERATOR
=
int	
 float	
 char	
 string	
 boolean	
 void	
int	
 OK	
 X	
 X	
 X	
 X	
 X	
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
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11
Type matching | Cube
OPERATOR
-
int	
 float	
 char	
 string	
 boolean	
 void	
int	
 float	
 X	
 X	
 X	
 X	
cube (- unary)
OPERATOR
+
int	
 float	
 char	
 string	
 boolean	
 void	
int	
 int	
 float	
 X	
 string	
 X	
 X	
float	
 float	
 float	
 X	
 string	
 X	
 X	
char	
 X	
 X	
 X	
 string	
 X	
 X	
string	
 string	
 string	
 string	
 string	
 string	
 X	
boolean	
 X	
 X	
 X	
 string	
 X	
 X	
void	
 X	
 X	
 X	
 X	
 X	
 X	
cube (- binary)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12
Type matching | Example 1
int x, y, z;
char p, q, r;
float a, b, c;
boolean foo;
void method() {
x = a * c + p;
}
x
*
+
a c p
=
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13
Type matching | Example 2
int a;
int c (int b) {
return b * 3 * 2 * 1 ;
}
void main () {
a = 1;
boolean a= c(14)/2 > 1;
}
cube for
matching types
symbol table
stack
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14
Programming Assignment 3
Level 2
Reviewing Type Matching
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15
Handwritten Notes
// Definition of the cube of types and operators
int cube [][][];
public static int OP_PLUS = 1;
public static int OP_MINUS = 2;
public static int OP_MULT = 3;
// …
public static int INTEGER = 1;
public static int FLOAT = 2;
// …
cube [OP_PLUS][INTEGER][INTEGER] = INTEGER;
cube [OP_PLUS][FLOAT][INTEGER] = FLOAT;
cube [OP_PLUS][INTEGER][FLOAT] = FLOAT;
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16
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 | 17
Assignment 2 | Code
C
1.
// except for the open parenthesis
SemanticAnalizer.pushStack(
tokens.get(currentToken).getToken()
);
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
Parser
1. Store in a flag (operatorWasUSed):
Did the operator ‘-’ exist?
2.
if (operatorWasUsed)
String x = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, “-” );
SemanticAnalizer.pushStack(result);
}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19
Parser
1. Store in a flag (twiceHere):
Did we pass this point twice?
3.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, y, operator );
SemanticAnalizer.pushStack(result);
twiceHere = false; // reset the flag
}
2. Store the operator that
creates the loop?
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20
Parser
1. Store in a flag (twiceHere):
Did we pass this point twice?
3.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, y, operator );
SemanticAnalizer.pushStack(result);
twiceHere = false; // reset the flag
}
2. Store the operator that
creates the loop?
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 21
Parser
1. Store in a flag (twiceHere):
Did we pass this point twice?
3.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, y, operator );
SemanticAnalizer.pushStack(result);
twiceHere = false; // reset the flag
}
2. Store the operator that
creates the loop?
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22
Parser
1. Store in a flag (operatorWasUSed):
Did the operator ‘-’ exist?
2.
if (operatorWasUsed)
String x = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, “!” );
SemanticAnalizer.pushStack(result);
}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 23
Parser
1. Store in a flag (twiceHere):
Did we pass this point twice?
2.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, y, “&” );
SemanticAnalizer.pushStack(result);
twiceHere = false; // reset the flag
}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 24
Parser
1. Store in a flag (twiceHere):
Did we pass this point twice?
2.
if (twiceHere)
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, y, “&” );
SemanticAnalizer.pushStack(result);
twiceHere = false; // reset the flag
}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 25
Assignment 2 | Code
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, y, “=” );
if (!result.equals(“OK”) {
error(2);
}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 26
Homework
Programming Assignment 3
CSE340 - Principles of Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
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.

More Related Content

PDF
201506 CSE340 Lecture 19
PDF
201506 CSE340 Lecture 20
PDF
201506 CSE340 Lecture 21
PDF
201506 CSE340 Lecture 22
PDF
201506 CSE340 Lecture 23
PDF
201506 CSE340 Lecture 13
PDF
201506 CSE340 Lecture 07
PDF
201505 CSE340 Lecture 05
201506 CSE340 Lecture 19
201506 CSE340 Lecture 20
201506 CSE340 Lecture 21
201506 CSE340 Lecture 22
201506 CSE340 Lecture 23
201506 CSE340 Lecture 13
201506 CSE340 Lecture 07
201505 CSE340 Lecture 05

What's hot (20)

PDF
201505 CSE340 Lecture 02
PDF
201506 CSE340 Lecture 15
PDF
201505 CSE340 Lecture 06
PDF
201506 CSE340 Lecture 10
PPT
Functions & Procedures [7]
PDF
Lecture 8 increment_and_decrement_operators
PDF
Arithmetic operator
DOCX
C programming Lab 1
DOC
C program to check leap year
DOCX
Programming fundamentals
PPTX
Machine language
PPTX
Arithmetic and logic operations in c
DOCX
Practical no 6
PPTX
Arithmetic and Logic instructions in Embedded C
DOCX
C programming Lab 2
PDF
Programming C Part 02
PPTX
C operators
PPTX
C operators
PPTX
11operator in c#
201505 CSE340 Lecture 02
201506 CSE340 Lecture 15
201505 CSE340 Lecture 06
201506 CSE340 Lecture 10
Functions & Procedures [7]
Lecture 8 increment_and_decrement_operators
Arithmetic operator
C programming Lab 1
C program to check leap year
Programming fundamentals
Machine language
Arithmetic and logic operations in c
Practical no 6
Arithmetic and Logic instructions in Embedded C
C programming Lab 2
Programming C Part 02
C operators
C operators
11operator in c#
Ad

Viewers also liked (20)

PDF
Week5-Group-J
PPT
簡介創用CC授權
PDF
Eurochap2010 final program
KEY
Urban Cottage + IceMilk Aprons
PDF
Phenomenal Oct 22, 2009
PDF
PDF
Final programme 27 06
PDF
Syst reninangiot pp cv aomi 02fev 2
PDF
PDF
PPT
Uzbekistan caving 2011
PDF
Deutsche Telekom BarCamp 03, 25 June 2009
PPT
Corporate taxation introduction
PPSX
Cluster 2
PPT
Login Seminars Blackboard Directions 2009
PDF
Thehub Milan Startup Weekend
PPT
Chapter 3 presentation
PPT
Syndrome metabolique et maladies vasculaires s novo
PDF
201505 CSE340 Lecture 04
Week5-Group-J
簡介創用CC授權
Eurochap2010 final program
Urban Cottage + IceMilk Aprons
Phenomenal Oct 22, 2009
Final programme 27 06
Syst reninangiot pp cv aomi 02fev 2
Uzbekistan caving 2011
Deutsche Telekom BarCamp 03, 25 June 2009
Corporate taxation introduction
Cluster 2
Login Seminars Blackboard Directions 2009
Thehub Milan Startup Weekend
Chapter 3 presentation
Syndrome metabolique et maladies vasculaires s novo
201505 CSE340 Lecture 04
Ad

Similar to 201506 CSE340 Lecture 18 (20)

PPTX
Unit 3 Compiler Design Regulation 2021.pptx
PPT
Semantic Analyzer.pptSemantic Analyzerpt
PDF
12TypeSystem.pdf
PPT
Data types and it's usage in c languages
PDF
Compiler Construction | Lecture 7 | Type Checking
PDF
Project presentation PPT.pdf this is help for student who doing this complier...
PDF
201506 CSE340 Lecture 17
PDF
Static Analysis
PDF
Monadic parsers in C++
PPTX
Compiler Design Notes for rgpv 6tth sem students
PDF
Declarative Semantics Definition - Static Analysis and Error Checking
PPTX
Type checking compiler construction Chapter #6
PDF
talk at Virginia Bioinformatics Institute, December 5, 2013
PDF
Extensible Operators and Literals for JavaScript
PDF
Syntax Analysis.pdf
PDF
Declare Your Language: Dynamic Semantics
Unit 3 Compiler Design Regulation 2021.pptx
Semantic Analyzer.pptSemantic Analyzerpt
12TypeSystem.pdf
Data types and it's usage in c languages
Compiler Construction | Lecture 7 | Type Checking
Project presentation PPT.pdf this is help for student who doing this complier...
201506 CSE340 Lecture 17
Static Analysis
Monadic parsers in C++
Compiler Design Notes for rgpv 6tth sem students
Declarative Semantics Definition - Static Analysis and Error Checking
Type checking compiler construction Chapter #6
talk at Virginia Bioinformatics Institute, December 5, 2013
Extensible Operators and Literals for JavaScript
Syntax Analysis.pdf
Declare Your Language: Dynamic Semantics

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
top salesforce developer skills in 2025.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Essential Infomation Tech presentation.pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
AI in Product Development-omnex systems
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
medical staffing services at VALiNTRY
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Migrate SBCGlobal Email to Yahoo Easily
top salesforce developer skills in 2025.pdf
Odoo POS Development Services by CandidRoot Solutions
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Essential Infomation Tech presentation.pptx
Design an Analysis of Algorithms I-SECS-1021-03
wealthsignaloriginal-com-DS-text-... (1).pdf
Reimagine Home Health with the Power of Agentic AI​
AI in Product Development-omnex systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
How Creative Agencies Leverage Project Management Software.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Digital Strategies for Manufacturing Companies
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
medical staffing services at VALiNTRY
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Odoo Companies in India – Driving Business Transformation.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...

201506 CSE340 Lecture 18

  • 1. CSE340 - Principles of Programming Languages Lecture 18: Semantic Analysis II Javier Gonzalez-Sanchez BYENG M1-38 Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2 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. 3.  Array’s indexes. Review that the indexes are integers. 4.  Conditions. Review that all expressions on the conditons return a boolean value. 5.  Return type. Review that the value returned by a method match the type of the method. 6.  Parameters. Review that the parameters in a method match in type and number with the declaration of the method.
  • 3. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3 2. Type Matching
  • 4. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4 Type matching | Example 1 int x, y, z; char p, q, r; float a, b, c; boolean foo; void method() { x = a * c + p; } x * + a c p =
  • 5. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5 Type matching | Cube fill one sheet for each operator in the language cube
  • 6. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6 Type matching | Cube OPERATOR int float char string boolean void int float char string boolean void cube
  • 7. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7 Type matching | Cube OPERATOR + int float char string boolean void int int float X string X X float float float X string X X char X X X string X X string string string string string string X boolean X X X string X X void X X X X X X cube
  • 8. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8 Type matching | Cube OPERATOR & int float char string boolean void int X X X X X X 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
  • 9. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9 Type matching | Cube OPERATOR < int float char string boolean void int boolean boolean X X X X float boolean boolean X X X X char X X X X X X string X X X X X X boolean X X X X X X void X X X X X X cube
  • 10. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10 Type matching | Cube OPERATOR = int float char string boolean void int OK X X X X X 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
  • 11. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11 Type matching | Cube OPERATOR - int float char string boolean void int float X X X X cube (- unary) OPERATOR + int float char string boolean void int int float X string X X float float float X string X X char X X X string X X string string string string string string X boolean X X X string X X void X X X X X X cube (- binary)
  • 12. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12 Type matching | Example 1 int x, y, z; char p, q, r; float a, b, c; boolean foo; void method() { x = a * c + p; } x * + a c p =
  • 13. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13 Type matching | Example 2 int a; int c (int b) { return b * 3 * 2 * 1 ; } void main () { a = 1; boolean a= c(14)/2 > 1; } cube for matching types symbol table stack
  • 14. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14 Programming Assignment 3 Level 2 Reviewing Type Matching
  • 15. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15 Handwritten Notes // Definition of the cube of types and operators int cube [][][]; public static int OP_PLUS = 1; public static int OP_MINUS = 2; public static int OP_MULT = 3; // … public static int INTEGER = 1; public static int FLOAT = 2; // … cube [OP_PLUS][INTEGER][INTEGER] = INTEGER; cube [OP_PLUS][FLOAT][INTEGER] = FLOAT; cube [OP_PLUS][INTEGER][FLOAT] = FLOAT;
  • 16. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16 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> ')'
  • 17. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17 Assignment 2 | Code C 1. // except for the open parenthesis SemanticAnalizer.pushStack( tokens.get(currentToken).getToken() );
  • 18. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18 Parser 1. Store in a flag (operatorWasUSed): Did the operator ‘-’ exist? 2. if (operatorWasUsed) String x = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, “-” ); SemanticAnalizer.pushStack(result); }
  • 19. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19 Parser 1. Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; // reset the flag } 2. Store the operator that creates the loop?
  • 20. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20 Parser 1. Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; // reset the flag } 2. Store the operator that creates the loop?
  • 21. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 21 Parser 1. Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; // reset the flag } 2. Store the operator that creates the loop?
  • 22. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22 Parser 1. Store in a flag (operatorWasUSed): Did the operator ‘-’ exist? 2. if (operatorWasUsed) String x = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, “!” ); SemanticAnalizer.pushStack(result); }
  • 23. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 23 Parser 1. Store in a flag (twiceHere): Did we pass this point twice? 2. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “&” ); SemanticAnalizer.pushStack(result); twiceHere = false; // reset the flag }
  • 24. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 24 Parser 1. Store in a flag (twiceHere): Did we pass this point twice? 2. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “&” ); SemanticAnalizer.pushStack(result); twiceHere = false; // reset the flag }
  • 25. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 25 Assignment 2 | Code String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “=” ); if (!result.equals(“OK”) { error(2); }
  • 26. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 26 Homework Programming Assignment 3
  • 27. CSE340 - Principles of Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu 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.