Upload
kenneth-greer
View
236
Download
0
Embed Size (px)
DESCRIPTION
NOEA/IT - CS Programme3 Some important Programming languages and their relationships
Citation preview
2008-10-02 NOEA/IT - CS Programme 1
On Languages
History of Programming Languages
About CompilersSyntax and Semantics
Describing Syntax:BNF and EBNF
Syntax Diagrams
2008-10-02 NOEA/IT - CS Programme 2
Læringsmål• Kunne forklare de forskellige opgaver, som en
compiler udfører• Kunne læse og forstå syntaksbeskrivelser på
forskellig form, især BNF• Kunne anvende regulære udtryk.• Kunne anvende tilstandsmaskiner til at
genkende regulære udtryk.• Kende sammenhængen mellem regulære
udtryk og tilstandsmaskiner.
2008-10-02 NOEA/IT - CS Programme 3
Some importantProgramminglanguages andtheir relationships
2008-10-02 NOEA/IT - CS Programme 4
The Tasks of the Compiler
CompilerSource Program Object Code
Error messages
2008-10-02 NOEA/IT - CS Programme 5
The Structure of a Compiler
sourcescanner
tokensparser
syntactic structure
SemanticRoutines
intermediate code optimizer Code
generatorobject code
2008-10-02 NOEA/IT - CS Programme 6
Syntax and Semantics
• The syntax of a language:– Rules for forming valid (legal) constructions
(sentences) in the language.• The semantics of a language:
– The meaning that is put into valid constructions in the language.
So syntax is about form and semantics about meaning.
2008-10-02 NOEA/IT - CS Programme 7
Examples
”The man drinks the beer that is cold”Syntactically and semantically correct.
”The man drink the beer that are coldly.” Not syntactically correct (but semantics?)
”The beer drinks the man, who is cold”Syntactically correct, but semantics is rubbish.
2008-10-02 NOEA/IT - CS Programme 8
Natural Languages vs. Context Free Languages
In context free languages semantics can be determined from syntax alone
”She was sitting on a fly.” ???
“Fly” has (at least) two different meanings.
Programming languages must be (almost context free)
2008-10-02 NOEA/IT - CS Programme 9
(Context Free) Grammars
• Context free grammars are used to describe the syntax of languages:
• Definition:• A set of terminal symbols• A set of non-terminal symbols.• A set of productions (or rules)
2008-10-02 NOEA/IT - CS Programme 10
Example 4.1P -> a, P -> bQ -> 0, Q -> 1R -> PQS -> R, S -> Rc
Terminals: a, b, 0, 1, c.Non-terminals (or meta symbols): P, Q, R and S.S is the defining meta symbol of the language:
S = {a0,a1,b0,b1,a0c,a1c,b0c,b1c}
2008-10-02 NOEA/IT - CS Programme 11
Syntax tree for “a0c”S
R P Q
a 0 c
Exercise 4.1Draw the syntax tree for b0c using the grammar from example 4.1.
P -> a, P -> bQ -> 0, Q -> 1R -> PQS -> R, S -> Rc
2008-10-02 NOEA/IT - CS Programme 12
Backus-Naur-Form (BNF)
C -> 0, C -> 1, ..., C -> 9U -> C, U -> CU
<digit> ::= 0│1│2│3│4│5│6│7│8│9<unsigned integer> ::= <digit>
│<digit><unsigned integer>
More readable?
2008-10-02 NOEA/IT - CS Programme 13
BNF
• <…> - non-terminals in sharp brackets• Terminals are written directly• ::= production• | alternatives
2008-10-02 NOEA/IT - CS Programme 14
EBNF (extended BNF)The grammar
<digit> ::= 0│1│2│3│4│5│6│7│8│9<unsigned integer> ::= <digit>│<digit><unsigned integer>
may in EBNF be written as
<unsigned integer> ::= <digit>{<digit>}
Curly brackets means zero or more,square brackets zero or one.
2008-10-02 NOEA/IT - CS Programme 15
Break for exercises:
Exercise 4.2Extend the grammar from example 4.2, so signed integers become valid.
Exercise 4.3Construct a grammar that defines the set of valid identifiers. An identifier is a sequence of characters beginning with aletter followed be zero or more letters or digits, for example exercise11a.
Exercise 4.4Rewrite the grammar from exercise 4.3 (identifiers) to EBNF.
2008-10-02 NOEA/IT - CS Programme 16
Syntax Diagrams
digit
910
Non-terminals in rectangles
Terminals in “roundangles”
You always follow the arrows
Exercise 4.5:Draw syntax diagrams for the grammar in exercise 4.2 (identifiers).
2008-10-02 NOEA/IT - CS Programme 17
The Syntax definition of Java• http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
– Choose:• Java Developer Resources• Documentation • The Java Language Specification
IfThenStatement:if ( Expression ) Statement
Non-terminals in italicTerminals in
fixed width
Productions: new line and indentation
2008-10-02 NOEA/IT - CS Programme 18
ExercisesExercise 5.1Write the grammar from exercise 4.2 (signed integers) in the Java notation.
Exersice 5.2Construct a syntax tree for the following Java statement (use the above link to the grammar).
if(x==100)y= 1.1*x;
2008-10-02 NOEA/IT - CS Programme 19
Syntax Diagrams for Java
?
2008-10-02 NOEA/IT - CS Programme 20
Exercise 5.3In 5.1 the grammar for signed integers was given:
SignedInteger:Signopt Digits
Sign: one of+ -
Digits:DigitDigits Digit
Digit:0NonZeroDigit
NonZeroDigit: one of1 2 3 4 5 6 7 8 9
Draw the corresponding syntax diagrams.
2008-10-02 NOEA/IT - CS Programme 21
Exercise 5.4Draw syntax diagrams which describe the Java While-statement.