Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of...
-
Upload
marjory-norton -
Category
Documents
-
view
223 -
download
0
Transcript of Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of...
![Page 1: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/1.jpg)
Syntax:
04/20/23 IT 327 1
Semantics:
Describe the structures of programs
Describe the meaning of programs
Programming Languages (formal languages)
-- How to describe them?-- How to use them? (machine and human)
Grammars -- Ambiguous (sometimes)
Textbook, manuals -- Confusing (always) solution: denotation semantics
(for nuts only)
solution: using unambiguous only
![Page 2: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/2.jpg)
English Grammar The man hit the ball. subject verb object
04/20/23 IT 327 2
The man saw the girl with a telescope. subject verb object
The purpose of grammar:
To have a device to generate all valid sentences in the target language (from a root).
To tell whether a sentence is valid.
Chomsky:
(old fashion)
![Page 3: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/3.jpg)
Noam Chomsky1928 -
04/20/23 IT 327 3
http://www.canada.com/nationalpost/news/issuesideas/story.html?id=1385b76d-6c34-4c22-942a-18b71f2c4a44
Syntactic Structures (1957)
Generative Grammar
A valid sentence is generated from a root according to some fixed rules (grammar).
![Page 4: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/4.jpg)
A generative grammar in Syntactic Structures
04/20/23 IT 327 4
S
NP
VP
T N
Verb
the | a
man | ball | car
hit | take | took | run | ran
NP
T
N
NP +
VP
Verb
+
+
…..
…..
root
terminal symbols
non-terminal symbols
![Page 5: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/5.jpg)
Syntactic Structures
04/20/23 IT 327 5
S
NP VP
T N Verb
the man
the ball
hit
NP
T N
the man hit the ball
![Page 6: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/6.jpg)
Backus-Naur Form, BNF
04/20/23 IT 327 6
<S> ::= <NP> <VP> <NP> ::= <T> <N><VP> ::= <V> <NP><T> ::= the<N> ::= man | ball<V> ::= hit | took
<S> ::= <NP> <V> <NP><NP> ::= <A> <N><V> ::= loves | hates|eats<A> ::= a | the<N> ::= dog | cat | rat
Grammar 1 Grammar 2
<S> ::= <NP> <V> <NP> | <NP> <VP> <NP> ::= <T> <N> | <A> <N><V> ::= loves | hates|eats |hit | took<A> ::= a | the<T> ::= the <N> ::= dog | cat | rat|man | ball
![Page 7: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/7.jpg)
Deviation: the sequence of processes that generate a sentence
04/20/23 IT 327 7
<S> <NP> <VP> <T> <N> <VP>the <N> <VP>the man <VP>the man <V> <NP>the man hit <NP>the man hit <T> <N> the man hit the <N> the man hit the ball
<S> ::= <NP> <VP> <NP> ::= <T> <N><VP> ::= <V> <NP><T> ::= the<N> ::= man | ball<V> := hit | took
Grammar 1
the man hit the ball
![Page 8: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/8.jpg)
04/20/23 IT 327 8
Parse: v. To break (a sentence) down into its component parts of speech with an explanation of the form, function, and syntactical relationship of each part.
(American Heritage Dict.)
the dog loves the cat
the loves dog the cat
loves the dog the cat
×
×
![Page 9: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/9.jpg)
A Parse Tree
04/20/23 IT 327 9
<S>
<NP> <V> <NP>
<A> <N><A> <N>
the dog the cat
lovesGrammar
<S> ::= <NP> <V> <NP><NP> ::= <A> <N><V> ::= loves | hates|eats<A> ::= a | the<N> ::= dog | cat | rat
“the loves dog the cat” doesn’t have a parse tree
![Page 10: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/10.jpg)
A grammar for Arithmetic Expression
04/20/23 IT 327 10
<exp> ::= <exp> + <exp> |
<exp> * <exp> |
( <exp> ) |
a | b | c
Example: ((a+b)*c)
Is this expression valid?
<exp>( <exp> )( <exp> * <exp> )(( <exp> ) * <exp> )((<exp> + <exp> ) * <exp> )((a + <exp> ) * <exp> )((a +b) * <exp> )((a+b)*c)
Yes
![Page 11: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/11.jpg)
A Parse Tree for ((a+b)*c)
04/20/23 IT 327 11
<exp>
<exp> + <exp>
( <exp> )
<exp> * <exp>
( <exp> )
a b
c
![Page 12: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/12.jpg)
Parse Trees for a+b*c
04/20/23 IT 327 12
<exp>
<exp> + <exp>
<exp> * <exp>
a b
c
<exp>
<exp> * <exp>
<exp> + <exp>
b c
a
?
What is the meaning of a+b*c
![Page 13: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/13.jpg)
Restrictions on Grammars
04/20/23 IT 327 13
Unrestricted Grammars(type-0)
Why context sensitive grammars have less restrictions than context free grammars?
Right/Left Linear Grammars(type-3)
Context Sensitive(type-1)
Context Free(type-2)
Diagram in terms of the sizes of the set of restrictions
![Page 14: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/14.jpg)
Chomsky Hierarchy
04/20/23 IT 327 14
Regular Expressions(type-3)
Computable (formal) languages(type-0)
Context-free languages(type-2)
Context-sensitive languages(type-1)
Diagram in terms of the sizes of the language families
![Page 15: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/15.jpg)
• A BNF grammar consists of four parts:
– The finite set of tokens (terminal symbols)
– The finite set of non-terminal symbols
– The start symbol
– The finite set of production rules
04/20/23 IT 327 15
<S> ::= <NP> <VP> <NP> ::= <T> <N><VP> ::= <V> <NP><T> ::= the<N> ::= man | ball<V> ::= hit | took
Grammars in BNF (Backus-Naur Form)
![Page 16: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/16.jpg)
Constructing Grammars
• Using divide and conquer to simplify the job. • Data types, variable names (identifiers)• One variable, one type (this is not grammar’s job to make sure)
04/20/23 IT 327 16
float a;
boolean a, b, c;
int a, b;
<var-dec>
![Page 17: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/17.jpg)
Primitive type names
Using divide and conquer
04/20/23 IT 327 17
<var-dec> ::= <type-name> <declarator-list> ;
<type-name> ::= boolean | byte | short | int | long | char | float | double
<declarator-list> ::= <declarator> | <declarator> , <declarator-list>
<declarator> ::= <variable-name> | <variable-name> = <expr>
![Page 18: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/18.jpg)
Tokens:
• How is such a program file (a sequence of characters) divided into a sequence of tokens?
04/20/23 IT 327 18
e.g. • identifiers (const, x, fact)
• keywords (if, const)
• operators (==)
• constants (123.4), etc.
• Programs stored in files are just sequences of characters, but we want to prepare them into tokens before further analysis.
Reserved words
Tokens are atoms of the program
![Page 19: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/19.jpg)
Lexical Structure And Phrase Structure
• Grammars so far have defined phrase structure: how a program is built from a sequence of tokens
• We also need to define lexical structure: how a text file is divided into tokens
04/20/23 IT 327 19
![Page 20: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/20.jpg)
Separate Grammars
• Usually there are two separate grammars– to construct a sequence of tokens from a file of
characters (Lexical Structure)
– to construct a parse tree from a sequence of tokens (Phrase Structure)
04/20/23 IT 327 20
<program-file> ::= <end-of-file> | <element> <program-file>
<element> ::= <token> | <one-white-space> | <comment><one-white-space> ::= <space> | <tab> | <end-of-line><token> ::= <identifier> | <operator> | <constant> | …
![Page 21: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/21.jpg)
Separate Compiler Passes
• Scanner tokens string• parser parse tree
• (more to do afterwards)
04/20/23 IT 327 21
![Page 22: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/22.jpg)
Historical Note #1
• Early languages sometimes did not separate lexical structure from phrase structure
– Early Fortran and Algol dialects allowed spaces anywhere, even in the middle of a keyword
– Other languages like PL/I or Early Fortran allow keywords to be used as identifiers
This makes them difficult to scan and parse
It also reduces readability
04/20/23 IT 327 22
![Page 23: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/23.jpg)
Historical Note #2
• Some languages have a fixed-format lexical structure -- column positions are significant
– One statement per line (i.e. per card)
– First few columns for statement label
– Etc.
• Early dialects of Fortran, Cobol, and Basic
• Almost all modern languages are free-format: column positions are ignored
04/20/23 IT 327 23
![Page 24: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/24.jpg)
Other Grammar Forms
• BNF variations
• EBNF variations
• Syntax diagrams
04/20/23 IT 327 24
![Page 25: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/25.jpg)
BNF Variations
• Some use or = instead of ::=
• Some leave out the angle brackets and use a distinct typeface for tokens
• Some allow single quotes around tokens, for example to distinguish ‘|’ as a token from | as a meta-symbol
04/20/23 IT 327 25
Sir, please Step away from the ASR-33
Interesting operator!!Or not!
![Page 26: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/26.jpg)
EBNF Variations
• Additional syntax to simplify some grammar chores:
– {x} to mean zero or more repetitions of x
– [x] to mean x is optional (i.e. x | <empty>)
– () for grouping
– | anywhere to mean a choice among alternatives
– Quotes around tokens, if necessary, to distinguish from meta-symbols
04/20/23 IT 327 26
![Page 27: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/27.jpg)
EBNF Examples
• Anything that extends BNF this way is called an Extended BNF: EBNF
• There are many variations
04/20/23 IT 327 27
<stmt-list> ::= {<stmt> ;}
<if-stmt> ::= if <expr> then <stmt> [else <stmt>]
<thing-list> ::= { (<stmt> | <declaration>) ;}
![Page 28: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/28.jpg)
Syntax Diagrams
• Syntax diagrams (“railroad diagrams”)
04/20/23 IT 327 28
if then elseexpr stmt stmtif-stmt
<if-stmt> ::= if <expr> then <stmt> else <stmt>
![Page 29: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/29.jpg)
Bypasses
04/20/23 IT 327 29
if then elseexpr stmt stmtif-stmt
<if-stmt> ::= if <expr> then <stmt> [else <stmt>]
![Page 30: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/30.jpg)
Branching
04/20/23 IT 327 30
exp
exp + exp
exp * exp
( exp )
a
b
c
<exp> ::= <exp> + <exp> | <exp> * <exp> | ( <exp> )| a | b | c
![Page 31: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/31.jpg)
Loops
04/20/23 IT 327 31
<exp> ::= <addend> {+ <addend>}
exp addend
+
![Page 32: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/32.jpg)
Syntax Diagrams, Pro and Con
• Easier for human to read (follow)
• Difficult to perceive the phrase structures (syntax tree)?
• Harder for machine to read (for automatic parser-generators)
04/20/23 IT 327 32
![Page 33: Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.](https://reader036.fdocuments.us/reader036/viewer/2022062422/56649ecf5503460f94bdc7cd/html5/thumbnails/33.jpg)
Conclusion• We use grammars to define programming
language syntax, both lexical structure and phrase structure
• Connection between theory and practice– Two grammars, two compiler passes– Parser-generators can produce code for those
two passes automatically from grammars (compiler tools)
04/20/23 IT 327 33