by Neng-Fa Zhou Programming language syntax Three aspects of languages – Syntax • How are sentences formed? – Semantics • What does a sentence mean? – Pragmatics • How to use the language? Only syntax can be described formally – Regular expressions and context-free grammars
22
Embed
By Neng-Fa Zhou Programming language syntax 4 Three aspects of languages –Syntax How are sentences formed? –Semantics What does a sentence mean? –Pragmatics.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
by Neng-Fa Zhou
Programming language syntax
Three aspects of languages – Syntax
• How are sentences formed?
– Semantics• What does a sentence mean?
– Pragmatics• How to use the language?
Only syntax can be described formally– Regular expressions and context-free grammars
by Neng-Fa Zhou
Regular expressions (RE)
The empty stringis a RE Every symbol in (alphabet) is a RE Let r and s be REs.
– r | s : or– rs : concatenation– (r)* : zero or more instances– (r)+ : one or more instances – (r)? : zero or one instance
by Neng-Fa Zhou
Precedence of operators
high
low
r* r+ r?
rs
r|s
all left associative Examples
= {a,b}1. a|b2. (a|b)(a|b)3. a*4. (a|b)*5. a| a*b
by Neng-Fa Zhou
Algebraic Properties of RE
by Neng-Fa Zhou
d1 r1
d2 r2
dn rn
....di is a RE over {d1,d2,...,di-1}
Regular Definitions
not recursive
by Neng-Fa Zhou
Examples
Identifiers
Decimal integers in Java
Hexadecimal integers
letter A | B | ... | Z | a | b | ... | zdigit 0 | 1 | ... | 9id letter ( letter | digit )*
DecimalNumeral 0 | nonZeroDigit digit*
HexaNumeral (0x | 0X) hexadigit+
by Neng-Fa Zhou
Lex
A tool for automatically generating lexical analyzers
– is a finite set of terminals– N is a finite set of non-terminals– P is a finite subset of production rules– S is the start symbol
G=( ,N,P,S)
by Neng-Fa Zhou
E T | E + T | E - TT F | T * F |T / FF id | (E)
CFG: Examples
Arithmetic expressions
Statements
IfStatement if E then Statement else Statement
by Neng-Fa Zhou
CFG vs. Regular Expressions
CFG is more expressive than RE– Every language that can be described by regular
expressions can also be described by a CFG
Example languages that are CFG but not RE– if-then-else statement, {anbn | n>=1}
Non-CFG– L1={wcw | w is in (a|b)*}– L2={anbmcndm | n>=1 and m>=1}
by Neng-Fa Zhou
Derivations
if
and then
*
* *
S*
is a sentential form
is a sentence if it contains only terminal symbols
by Neng-Fa Zhou
Derivations
leftmost derivation
Rightmost derivation
if is a string of terminals
if is a string of terminals
by Neng-Fa Zhou
Parse Trees
A parse tree is any tree in which– The root is labeled with S– Each leaf is labeled with a token a or – Each interior node is labeled by a nonterminal– If an interior node is labeled A and has children