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

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
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
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
(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
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 

Recently uploaded (20)

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
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
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
(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...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

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.