Presentation is loading. Please wait.

Presentation is loading. Please wait.

2008-10-02NOEA/IT - CS Programme1 On Languages History of Programming Languages About Compilers Syntax and Semantics Describing Syntax: BNF and EBNF Syntax.

Similar presentations


Presentation on theme: "2008-10-02NOEA/IT - CS Programme1 On Languages History of Programming Languages About Compilers Syntax and Semantics Describing Syntax: BNF and EBNF Syntax."— Presentation transcript:

1 2008-10-02NOEA/IT - CS Programme1 On Languages History of Programming Languages About Compilers Syntax and Semantics Describing Syntax: BNF and EBNF Syntax Diagrams

2 2008-10-02NOEA/IT - CS Programme2 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.

3 2008-10-02NOEA/IT - CS Programme3 Some important Programming languages and their relationships

4 2008-10-02NOEA/IT - CS Programme4 The Tasks of the Compiler Compiler Source ProgramObject Code Error messages

5 2008-10-02NOEA/IT - CS Programme5 The Structure of a Compiler source scanner tokens parser syntactic structure Semantic Routines intermediate code optimizer Code generator object code

6 2008-10-02NOEA/IT - CS Programme6 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.

7 2008-10-02NOEA/IT - CS Programme7 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.

8 2008-10-02NOEA/IT - CS Programme8 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)

9 2008-10-02NOEA/IT - CS Programme9 (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)

10 2008-10-02NOEA/IT - CS Programme10 Example 4.1 P -> a, P -> b Q -> 0, Q -> 1 R -> PQ S -> 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}

11 2008-10-02NOEA/IT - CS Programme11 Syntax tree for “a0c” S R PQ a0c Exercise 4.1 Draw the syntax tree for b0c using the grammar from example 4.1. P -> a, P -> b Q -> 0, Q -> 1 R -> PQ S -> R, S -> Rc

12 2008-10-02NOEA/IT - CS Programme12 Backus-Naur-Form (BNF) C -> 0, C -> 1,..., C -> 9 U -> C, U -> CU ::= 0│1│2│3│4│5│6│7│8│9 ::= │ More readable?

13 2008-10-02NOEA/IT - CS Programme13 BNF - non-terminals in sharp brackets Terminals are written directly ::=production |alternatives

14 2008-10-02NOEA/IT - CS Programme14 EBNF (extended BNF) The grammar ::= 0│1│2│3│4│5│6│7│8│9 ::= │ may in EBNF be written as ::= { } Curly brackets means zero or more, square brackets zero or one.

15 2008-10-02NOEA/IT - CS Programme15 Break for exercises: Exercise 4.2 Extend the grammar from example 4.2, so signed integers become valid. Exercise 4.3 Construct a grammar that defines the set of valid identifiers. An identifier is a sequence of characters beginning with a letter followed be zero or more letters or digits, for example exercise11a. Exercise 4.4 Rewrite the grammar from exercise 4.3 (identifiers) to EBNF.

16 2008-10-02NOEA/IT - CS Programme16 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).

17 2008-10-02NOEA/IT - CS Programme17 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 italic Terminals in fixed width Productions: new line and indentation

18 2008-10-02NOEA/IT - CS Programme18 Exercises Exercise 5.1 Write the grammar from exercise 4.2 (signed integers) in the Java notation. Exersice 5.2 Construct a syntax tree for the following Java statement (use the above link to the grammar). if(x==100) y= 1.1*x;

19 2008-10-02NOEA/IT - CS Programme19 Syntax Diagrams for Java ?

20 2008-10-02NOEA/IT - CS Programme20 Exercise 5.3 In 5.1 the grammar for signed integers was given: SignedInteger: Sign opt Digits Sign: one of + - Digits: Digit Digits Digit Digit: 0 NonZeroDigit NonZeroDigit: one of 1 2 3 4 5 6 7 8 9 Draw the corresponding syntax diagrams.

21 2008-10-02NOEA/IT - CS Programme21 Exercise 5.4 Draw syntax diagrams which describe the Java While-statement.


Download ppt "2008-10-02NOEA/IT - CS Programme1 On Languages History of Programming Languages About Compilers Syntax and Semantics Describing Syntax: BNF and EBNF Syntax."

Similar presentations


Ads by Google