1 / 23

ITEC 380

ITEC 380. Organization of programming languages Lecture 2 – Grammar / Language capabilities. Review. Definition / purpose of a language Paradigms Compilation / Interpretation Stages of compilation. Objectives. Grammars Rules of languages. Compiling.

donnan
Download Presentation

ITEC 380

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Objectives • Grammars • Rules of languages

  4. 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. 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. Are the following sentences valid? I saw a dog chasing the ball. The dog chased the cat the dog chased the cat Example grammar • Nouns: either cat or dog • Represented by <noun> ::= cat|dog • Verbs • <verb> ::= saw | chased • Articles: • <article> ::= a | the • Noun phrases: • <nounPhase> ::= <article> <noun> • Sentences: • <sentence> ::= <nounPhase> <verb> <nounPhase>

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

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

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

  10. 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. EBNF • Slightly more rule happy • Mainly geared to make it easier to write • Rules • [] means optional • {} means 0 or more repetitions • (|) means alternatives

  12. 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. Details What is interesting about the grammar S --> aS | a or S ::= aS| a • 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

  14. Other languages • L --> aLbLc | ab | bc • Whatareexamplesentencesgivenbythislanguage? • 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. 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. Numbers • Example • <integer> --> <digit><integer> | <digit> • <digit> --> 0 | 1 | 2 | 3 ... | 9 • <ident> --> <letter><letter><letterOrDigitSeq> • <letterOrDigitSeq> --> <letterOrDigit>|<letterOrDigit><letterOrDigitSeq> <letterOrDigit> --> <letter> | <digit> • <letter> --> 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. Simple language How would we parse if .. then if .. then .. else .. • <program> --> program <ident> is <stmtList> end; • <stmtList> --> <stmt> | <stmt> <stmtList> • <stmt> --> <ident> := <expr> ; | if <boolExp> then <stmtList> end if ; | if <boolExp> then <stmtList> else <stmtList> end if ; | begin <stmt> end ;| ... • <expr> --> <expr> + <expr> | <ident> | <integer> | ( <expr> ) | ... <boolExp> --> true|false <ident> --> <letter> | <letter><letterOrDigit><ident>| <letter><letterOrDigit>

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

  19. 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. Abstract / Concrete • Two methods for handling information • Keep track of non-terminals • Remove non-terminals • Terminals become leaf nodes • Operations become interior nodes

  21. 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. Review • Grammars • BNF/EBNF • Parse trees • How to define / recognize a language • Specifics • Compilers

  23. Next week • Functional programming - Lisp

More Related