









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the fundamentals of programming languages, focusing on syntax and semantics. Syntax refers to the rules and symbols used to write legal programs, while semantics deals with the meaning of these programs. Describing language syntax using lexical and grammar syntax, the importance of syntax description, and semantics description using informal and formal methods. It also discusses context-free grammars and their role in defining language aspects.
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










Syntax and Semantics
date ::= dd/dd/dddd d = 0|1|2|3|4|5|6|7|8|
01/02/2005 => Jan 02, 2005 (or Feb 01,2005)?
Why Describing Syntax?
Describing Semantics
Natural language explanation for each syntax rule
Associate attributes (values) with each grammar symbol
Associate semantics rules with each grammar rule
Interpret the language on an abstract machine or using
another language
Define language constructs as mathematical functions
Define properties (invariance) of language constructs
What language aspects can be
defined by CFG?
Declaration of variables is in the context (symbol table)
int w;
0 = w;
for (w = 1; w < 100; w = 2w)
a = “c” + 3;
a = “c” + w
Derivations and Parse Trees
(Semantics of CFG)
Each replacement follows a production rule
One or more derivations for each program
e=> ee* => e+ee* =>n+ee=>d+ee=> 5+ee* =>5+ne=>5+nd
e=>5+dde=>5+1de=> 5+15e* =>…=> 5+15*
E=> e+e =>…=> 5+e => 5+ee* =>…=> 5+15e* =>…=> 5+15*
e
e
e
e e
n
d
n
d
n
d
n
d
n
d
e
e
e
n e
d
n
d
n
d
n
d
n d
e
Parse trees:
Abstract vs. Concrete Syntax
Example: different notations of expressions
Prefix + 5 * 15 20
Infix 5 + 15 * 20
Postfix 5 15 20 * +
Identifies only the meaningful components
What is the operation and which are the operands?
e
e
e
e
e
Parse Tree for
Abstract Syntax Tree for 5 + 15 * 20
cs3723 11
Abstract syntax trees
They define the meaning of the interior (parent) node
If-then-else
Ambiguous Grammars
Multiple choices of production rules during derivation
Multiple ways to interpret a program
e
e
e
e e
n
d
n
d
n
d
n
d
n
d
e
e
e
n e
d
n
d
n
d
n
d
n d
e
Rewrite ambiguous Grammars
Original grammar: e ::= n | e+e | e−e | e * e | e / e
Precedence and associativity
* / >> + - all operators are left associative
Derivation for n+n*n
e=>e+e=>n+e=>n+ee=>n+ne=>n+n*n
Alternative grammar E ::= E + T | E – T | T
T ::= T * F | T / F | F
F ::= n
Derivation for n + n * n
E=>E+T=>T+T=>F+T=>n+T=>n+TF=>n+FF=>n+nF=>n+nn
How to modify the grammar if
+ and - has high precedence than * and /
All operators are right associative
Additional exercises
0',1',...,9'),(', )',;' and `->' eg., 3->4 is an edge from node 3' to node4'
Eg. ( 1->2; 2->5; 5->1)
Write a parse tree and an abstract syntax tree
for ( 1->2; 2->5; 5->1)
Additional Exercises
(practice on your own)
“0|1”, “0”, (01|10) are in the languages
“0|” and “*0” are not in the language