SlideShare a Scribd company logo
1 of 27
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

What's hot (20)

201505 CSE340 Lecture 02
201505 CSE340 Lecture 02201505 CSE340 Lecture 02
201505 CSE340 Lecture 02
 
201506 CSE340 Lecture 15
201506 CSE340 Lecture 15201506 CSE340 Lecture 15
201506 CSE340 Lecture 15
 
201505 CSE340 Lecture 06
201505 CSE340 Lecture 06201505 CSE340 Lecture 06
201505 CSE340 Lecture 06
 
Prefix Postfix
Prefix PostfixPrefix Postfix
Prefix Postfix
 
201506 CSE340 Lecture 10
201506 CSE340 Lecture 10201506 CSE340 Lecture 10
201506 CSE340 Lecture 10
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 
Lecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operatorsLecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operators
 
Arithmetic operator
Arithmetic operatorArithmetic operator
Arithmetic operator
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 
C program to check leap year
C program to check leap year C program to check leap year
C program to check leap year
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
Machine language
Machine languageMachine language
Machine language
 
Arithmetic and logic operations in c
Arithmetic and logic operations in cArithmetic and logic operations in c
Arithmetic and logic operations in c
 
Practical no 6
Practical no 6Practical no 6
Practical no 6
 
Arithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded CArithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded C
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
Programming C Part 02
Programming C Part 02Programming C Part 02
Programming C Part 02
 
C operators
C operators C operators
C operators
 
C operators
C operatorsC operators
C operators
 
11operator in c#
11operator in c#11operator in c#
11operator in c#
 

Viewers also liked

Week5-Group-J
Week5-Group-JWeek5-Group-J
Week5-Group-Js1160114
 
簡介創用CC授權
簡介創用CC授權簡介創用CC授權
簡介創用CC授權Chou Emily
 
Eurochap2010 final program
Eurochap2010 final programEurochap2010 final program
Eurochap2010 final programsfa_angeiologie
 
Urban Cottage + IceMilk Aprons
Urban Cottage + IceMilk ApronsUrban Cottage + IceMilk Aprons
Urban Cottage + IceMilk ApronsIceMilk Aprons
 
Phenomenal Oct 22, 2009
Phenomenal Oct 22, 2009Phenomenal Oct 22, 2009
Phenomenal Oct 22, 2009etalcomendras
 
Syst reninangiot pp cv aomi 02fev 2
Syst reninangiot pp cv   aomi 02fev 2Syst reninangiot pp cv   aomi 02fev 2
Syst reninangiot pp cv aomi 02fev 2sfa_angeiologie
 
Uzbekistan caving 2011
Uzbekistan caving 2011Uzbekistan caving 2011
Uzbekistan caving 2011Yura Taras
 
Deutsche Telekom BarCamp 03, 25 June 2009
Deutsche Telekom BarCamp 03, 25 June 2009Deutsche Telekom BarCamp 03, 25 June 2009
Deutsche Telekom BarCamp 03, 25 June 2009Jackson Bond
 
Corporate taxation introduction
Corporate taxation introductionCorporate taxation introduction
Corporate taxation introductiondphil002
 
Login Seminars Blackboard Directions 2009
Login Seminars Blackboard Directions 2009Login Seminars Blackboard Directions 2009
Login Seminars Blackboard Directions 2009Ohio LETC
 
Thehub Milan Startup Weekend
Thehub   Milan Startup WeekendThehub   Milan Startup Weekend
Thehub Milan Startup WeekendThe Hub Milan
 
Chapter 3 presentation
Chapter 3 presentationChapter 3 presentation
Chapter 3 presentationdphil002
 
Syndrome metabolique et maladies vasculaires s novo
Syndrome metabolique et maladies vasculaires s novoSyndrome metabolique et maladies vasculaires s novo
Syndrome metabolique et maladies vasculaires s novosfa_angeiologie
 

Viewers also liked (20)

Week5-Group-J
Week5-Group-JWeek5-Group-J
Week5-Group-J
 
201001 amt
201001 amt201001 amt
201001 amt
 
簡介創用CC授權
簡介創用CC授權簡介創用CC授權
簡介創用CC授權
 
Eurochap2010 final program
Eurochap2010 final programEurochap2010 final program
Eurochap2010 final program
 
Urban Cottage + IceMilk Aprons
Urban Cottage + IceMilk ApronsUrban Cottage + IceMilk Aprons
Urban Cottage + IceMilk Aprons
 
Phenomenal Oct 22, 2009
Phenomenal Oct 22, 2009Phenomenal Oct 22, 2009
Phenomenal Oct 22, 2009
 
Tabado
TabadoTabado
Tabado
 
Final programme 27 06
Final programme 27 06Final programme 27 06
Final programme 27 06
 
Syst reninangiot pp cv aomi 02fev 2
Syst reninangiot pp cv   aomi 02fev 2Syst reninangiot pp cv   aomi 02fev 2
Syst reninangiot pp cv aomi 02fev 2
 
201106 WICSA
201106 WICSA201106 WICSA
201106 WICSA
 
201107 ICALT
201107 ICALT201107 ICALT
201107 ICALT
 
Uzbekistan caving 2011
Uzbekistan caving 2011Uzbekistan caving 2011
Uzbekistan caving 2011
 
Deutsche Telekom BarCamp 03, 25 June 2009
Deutsche Telekom BarCamp 03, 25 June 2009Deutsche Telekom BarCamp 03, 25 June 2009
Deutsche Telekom BarCamp 03, 25 June 2009
 
Corporate taxation introduction
Corporate taxation introductionCorporate taxation introduction
Corporate taxation introduction
 
Cluster 2
Cluster 2Cluster 2
Cluster 2
 
Login Seminars Blackboard Directions 2009
Login Seminars Blackboard Directions 2009Login Seminars Blackboard Directions 2009
Login Seminars Blackboard Directions 2009
 
Thehub Milan Startup Weekend
Thehub   Milan Startup WeekendThehub   Milan Startup Weekend
Thehub Milan Startup Weekend
 
Chapter 3 presentation
Chapter 3 presentationChapter 3 presentation
Chapter 3 presentation
 
Syndrome metabolique et maladies vasculaires s novo
Syndrome metabolique et maladies vasculaires s novoSyndrome metabolique et maladies vasculaires s novo
Syndrome metabolique et maladies vasculaires s novo
 
201505 CSE340 Lecture 04
201505 CSE340 Lecture 04201505 CSE340 Lecture 04
201505 CSE340 Lecture 04
 

Similar to 201506 CSE340 Lecture 18

Artificial software diversity: automatic synthesis of program sosies
Artificial software diversity: automatic synthesis of program sosiesArtificial software diversity: automatic synthesis of program sosies
Artificial software diversity: automatic synthesis of program sosiesFoCAS Initiative
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfjanakim15
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdSyed Mustafa
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CSyed Mustafa
 
Code quailty metrics demystified
Code quailty metrics demystifiedCode quailty metrics demystified
Code quailty metrics demystifiedJeroen Resoort
 
Project in TLE
Project in TLEProject in TLE
Project in TLEPGT_13
 

Similar to 201506 CSE340 Lecture 18 (20)

201506 CSE340 Lecture 17
201506 CSE340 Lecture 17201506 CSE340 Lecture 17
201506 CSE340 Lecture 17
 
201506 CSE340 Lecture 14
201506 CSE340 Lecture 14201506 CSE340 Lecture 14
201506 CSE340 Lecture 14
 
201506 CSE340 Lecture 11
201506 CSE340 Lecture 11201506 CSE340 Lecture 11
201506 CSE340 Lecture 11
 
201506 CSE340 Lecture 09
201506 CSE340 Lecture 09201506 CSE340 Lecture 09
201506 CSE340 Lecture 09
 
201506 CSE340 Lecture 12
201506 CSE340 Lecture 12201506 CSE340 Lecture 12
201506 CSE340 Lecture 12
 
201801 CSE240 Lecture 07
201801 CSE240 Lecture 07201801 CSE240 Lecture 07
201801 CSE240 Lecture 07
 
201801 CSE240 Lecture 03
201801 CSE240 Lecture 03201801 CSE240 Lecture 03
201801 CSE240 Lecture 03
 
201506 - CSE340 Lecture 08
201506 - CSE340 Lecture 08201506 - CSE340 Lecture 08
201506 - CSE340 Lecture 08
 
201707 CSE110 Lecture 04
201707 CSE110 Lecture 04   201707 CSE110 Lecture 04
201707 CSE110 Lecture 04
 
Artificial software diversity: automatic synthesis of program sosies
Artificial software diversity: automatic synthesis of program sosiesArtificial software diversity: automatic synthesis of program sosies
Artificial software diversity: automatic synthesis of program sosies
 
201505 CSE340 Lecture 03
201505 CSE340 Lecture 03201505 CSE340 Lecture 03
201505 CSE340 Lecture 03
 
201505 - CSE340 Lecture 01
201505 - CSE340 Lecture 01201505 - CSE340 Lecture 01
201505 - CSE340 Lecture 01
 
201707 CSE110 Lecture 15
201707 CSE110 Lecture 15   201707 CSE110 Lecture 15
201707 CSE110 Lecture 15
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
201707 CSE110 Lecture 08
201707 CSE110 Lecture 08  201707 CSE110 Lecture 08
201707 CSE110 Lecture 08
 
Code quailty metrics demystified
Code quailty metrics demystifiedCode quailty metrics demystified
Code quailty metrics demystified
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Recently uploaded

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 

Recently uploaded (20)

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 

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.