Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Language Syntax 2

Similar presentations


Presentation on theme: "Programming Language Syntax 2"— Presentation transcript:

1 Programming Language Syntax 2

2 Think-Pair-Share Activity
Assuming the following INTEGER regex: Try to build a regex that matches arithmetic expressions, such as: 55 2 + 5 4 * 5 / -3 (9 * 4)/(2 - +4) ((4 + 7) * 10) * (69 + 7) / (44 - ( ) * +5)

3 Here’s one way Except this isn’t a regex
Regexes cannot have recursive constructs It’s actually a context-free grammar (CFG) Like regexes with recursion Expressed (more or less) in Backus-Naur Form (BNF)

4 But how do you tell ANTLR what your language is like?
Recall from last time… ANTLR generates for you But how do you tell ANTLR what your language is like?

5 … You specify token patterns using Regular Expressions
You specify patterns of tokens using a Context-Free Grammar

6 Important distinctions between regexes and CFG rules in ANTLR
Naming: Regex names start with uppercase letter CFG-rule names start with lowercase letter Character versus token handling: Regexes process stream of characters CFG rules process stream of tokens

7 Will this regex match these strings?
“4444”? “ ”? Yes No

8 Will this CFG production match these strings?
“4444”? “ ”? Yes Yes

9 Backus-Naur Form (BNF)
ANTLR BNF

10 Extended BNF (EBNF) ANTLR BNF Things not in BNF EBNF

11 CFG Terminology terminals productions non-terminal

12 CFG Derivation Series of replacement operations that shows how to derive a string of terminals from the start symbol

13 Derivation Example CFG: String to derive:

14 CFG: String to derive: Derivation:

15 Parse Tree: Graphical Representation of Derivation
Can you think of another possible derivation? Hint: This one is a “right-most” derivation

16 Here’s a “left-most” derviation
A grammar with multiple possible derivations is ambiguous Makes generating parser more difficult

17 Two concepts important to expressions
Associativity: Group based on L-to-R order means (10 - 4) versus (4 - 3) Precedence: Group based on operator 3 + 4 * 5 means (4 * 5) versus (3 + 4) * 5

18 Think-Pair-Share Activity
Rewrite this CFG to be unambiguous Left associative Multiplication/division have higher precedence than addition/subtraction

19 Solution Create parse tree for: 3 + 4 * 5

20 Parse Tree for * 5

21 Parse Tree for

22 What’s next? Homework 1 assigned!


Download ppt "Programming Language Syntax 2"

Similar presentations


Ads by Google