Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 380 Organization of programming languages Lecture 2 – Grammar / Language capabilities.

Similar presentations


Presentation on theme: "ITEC 380 Organization of programming languages Lecture 2 – Grammar / Language capabilities."— Presentation transcript:

1 ITEC 380 Organization of programming languages Lecture 2 – Grammar / Language capabilities

2 Grammar Review Definition / purpose of a language Paradigms Compilation / Interpretation Stages of compilation

3 Grammar Objectives Grammars Rules of languages

4 Grammar Compiling You know the basic stages of what goes on when a program is compiled Details details –How to recognize a sentence in a language Rules for recognizing a language

5 Grammar Language Made up of any possible sentences made using a grammar All sentences made by a grammar have to be part of the language Grammars provide rules for what is possible

6 Grammar Example grammar Nouns: either cat or dog –Represented by ::= cat|dog Verbs – ::= saw | chased Articles: – ::= a | the Noun phrases: – ::= Sentences: – ::= Are the following sentences valid? I saw a dog chasing the ball. The dog chased the cat the dog chased the cat

7 Grammar Implementation How would we implement a parser for the previous language? Thoughts / ideas?

8 Grammar One way Input -getToken() Input -getToken() main parseLines() main parseLines() parseLines nounPhrase article noun verb Recursive decent parser is probably the easiest way to implement

9 Grammar Issues Not an easy task Grammars are quite large for well known languages –http://docs.oracle.com/javase/specs/jls/se7/ht ml/jls-18.html Benefits –Implementation of a language –Tools that provide additional features

10 Grammar BNF Backus Naur Form Each rule has one non-terminal at the left hand side Makes implementation easier than –Noun|Verb ::= cat dog catch Does not solve all issues though –Language features / how the grammar is parsed

11 Grammar EBNF Slightly more rule happy Mainly geared to make it easier to write Rules –[] means optional –{} means 0 or more repetitions –(|) means alternatives

12 Grammar Examples BNF: –E -> E + T | E - T | T –T -> T * F | T / F | F –F -> a | b | c EBNF: –E -> T { (+ | -) T } –T -> F { (* | /) F } –F -> a | b | c

13 Grammar Details Terminals –Keywords Variables –Non-Terminals Substitution Rules –Drill down Start Symbol –Where does it all begin? Litmus test –Grammar must provide all possible sentences in a language –All sentences produced by a grammar must be in a language What is interesting about the grammar S --> aS | a or S ::= aS | a

14 Grammar Other languages L --> aLbLc | ab | bc What are example sentences given by this language? S --> aSBA S --> abA AB --> BA bB --> bb bA --> ba aA --> aa Is this a BNF? What are some issues with this language? Context sensitivity

15 Grammar Ambiguity Example language –A --> aA | Aa | a What are some issues with this langauge that might come up with the sentence aa? Other issues –Precedence i.e. and before or

16 Grammar Numbers Example --> | --> 0 | 1 | 2 | 3... | 9 --> --> | --> | --> a | b | c... What would we add to handle floating point numbers? What would be the starting point for this grammar? What changes would need to be made for implementation?

17 Grammar Simple language --> program is end; --> | --> := ; | if then end if ; | if then else end if ; | begin end ;|... --> + | | | ( ) |... --> true|false --> | | How would we parse if.. then if.. then.. else..

18 Grammar Trees What do you remember from your earlier programming courses about tree data structures?

19 Grammar Methods Use recursive functions to read / handle code generation Use recursive functions to build up a tree data structure Prune data structure using optimization Use it to generate code by going either bottom to top (left to right or right to left), or top down

20 Grammar Abstract / Concrete Two methods for handling information Keep track of non-terminals Remove non-terminals –Terminals become leaf nodes –Operations become interior nodes

21 Grammar Types LL –Top down context free grammar –Goes from left to right –Specify number of lookahead characters –LL(1) – Easy to make LR –Bottom up non-ambiguous context free grammar –Linear time –Often generated by tools

22 Grammar Review Grammars BNF/EBNF Parse trees How to define / recognize a language Specifics –Compilers

23 Grammar Next week Functional programming - Lisp


Download ppt "ITEC 380 Organization of programming languages Lecture 2 – Grammar / Language capabilities."

Similar presentations


Ads by Google