1 / 58

Theoretical Concepts For The Parsing Assignment

Theoretical Concepts For The Parsing Assignment. A return to the compilation process Parsing and Formal grammars Divide and conquer through recursion. Machine language program. Computer program. Compiler. anything .p. a.out. input. output. Compilation. Parts Of The Compiler. Back end.

devon
Download Presentation

Theoretical Concepts For The Parsing Assignment

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. Theoretical Concepts For The Parsing Assignment A return to the compilation process Parsing and Formal grammars Divide and conquer through recursion

  2. Machine language program Computer program Compiler anything.p a.out input output Compilation

  3. Parts Of The Compiler Back end Front end Lexical analyzer Syntax analyzer Semantic analyzer Translates to target language

  4. The Lexical Analyzer • Groups symbols together into entities w h i l e while

  5. Syntax Analyzer (Parser) • Analyzes the structure of the program in order to group together related symbols while (i < 5) { statement; statement; : } while (i < 5) statement;

  6. Semantic analyzer • Determines the meaning • int num; • double db;

  7. Assignment 4 • With a given English phrase your program must perform: • A lexical analysis • A syntactical analysis (parse the phrase) • Needs a formal grammar

  8. User types in a sentence This stupid horse smokes. 1 Perform a lexical analysis This stupid horse smokes . This stupid horse smokes article adjective noun verb Is this sentence grammatically correct according to the rules of our syntax? 1 The use of nicotine products by animals is not endorsed by this instructor or the University of Calgary A Perspective Into Assignment 4 Perform a syntactical analysis .

  9. or Jack and Jill Bob and Bill went up the hill. and and Jack Jill Bob Bill went up the hill. A Perspective Into Assignment 4 (2) • Conjunctions must be handled recursively Jack and Jill or Bob and Bill went up the hill.

  10. Backus-Naur Form (BNF) • An example of a formal grammar • Can be used in the fourth assignment to specify the syntax of the language (grammatically correct) • Introduced by Jim Backus and developed by Pete Naur to specify the syntax rules for Algol 60

  11. BNF: General Examples • Example one: • <A> ::= <B1> <B2> <B3>…<Bn> • Example two (alternatives): • <A> ::= <B1> <B2> <B3> | <B2> <B4> <B1> • Example three (program specification): • x = x + 1 • <Assignment statement> ::= <variable> < = > <expression>

  12. BNF: Assignment 4 • (The following specifications come from the main 233 course web page: www.cpsc.ucalgary.ca/~becker/233) • <STATEMENT> ::= <Sentence> <PUNCT> • <Sentence> ::= <NounPhrase> <VerbPhrase> | • <NounPhrase> <VerbPhrase> <Conjunction> <Sentence> • <NounPhrase> ::= <ProNoun> | <ProperNoun> | <Article> <AdjectiveList> <Noun> | <Article> <Noun> | <Noun>

  13. BNF: Assignment 4 • <VerbPhrase> ::= <AdverbList> <Verb> | <Verb> <AdverbList> | • <Verb> <NounPhrase> <AdverbList> | • <AdverbList> <Verb> <NounPhrase> | • <Verb> • <AdjectiveList> ::= <AdjectiveList> <Adjective> | <nothing> • <AdverbList> ::= <AdverbList> <Conjunction> <Adverb> | <Adverb>

  14. Syntax Diagrams • An alternative method for representing a formal language <> <>

  15. <A> ::= Bi <A> ::= B1 B2 B3 B2 B4 B1 Syntax Diagrams: General Examples • Example one: • Example two: i = 1, 2, 3…n

  16. variable = ::= expression Syntax Diagrams: General Examples (2) • Example three (program specification) • x = x + 1 <Assignment statement>

  17. Syntax Diagrams: Assignment 4

  18. Syntax Diagrams: Assignment 4 (2)

  19. Syntax Diagrams: Assignment 4 (3)

  20. Syntax Diagrams: Assignment 4 (4)

  21. Divide And Conquer • Split the problem into sub-problems (through recursive calls) • Continue splitting each of the sub-problems into smaller parts until you cannot split the problem up any further • Solve each of the sub-problems and combine the solutions yielding the solution to the original problem

  22. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8]

  23. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1

  24. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 [0] [1] [2] [3]

  25. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 [0] [1] [2] [3] +

  26. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 [0] [1] [2] [3] + [0]

  27. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 [0] [1] [2] [3] + 1

  28. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 [0] [1] [2] [3] 1 +

  29. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 [0] [1] [2] [3] 1 + [2] [3]

  30. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 [0] [1] [2] [3] 1 + 1

  31. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 [0] [1] [2] [3] 1 + 1

  32. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 1 2

  33. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1

  34. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 [5] [6] [7] [8]

  35. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 [5] [6] [7] [8] 1

  36. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 [5] [6] [7] [8] 1 [5]

  37. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 [5] [6] [7] [8] 1 0

  38. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 [5] [6] [7] [8] 1 [7] [8]

  39. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 [5] [6] [7] [8] 1 1

  40. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 [5] [6] [7] [8] 1 + 1

  41. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 2

  42. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 + 2

  43. Divide And Conquer: An Example [0] [1] [2] [3] [4] [5] [6] [7] [8] 2 + 1 + 2

  44. Divide And Conquer: An Example Final result after recursive calls = 5

  45. NumberList Adder -list: char [] -low: int -high: int + NumberList () + displayList () +defaultInitialization () • asciiToInteger () • divideAndAdd () Driver Divide And Conquer Example

  46. Divide And Conquer Example • The complete source and executable files can be found in Unix under the directory: • /home/profs/tamj/233/examples/recursion

  47. The Driver Class • class Driver • { • public static void main (String [] argv) • { • NumberList list = new NumberList (); • Adder listAdder = new Adder(); • int total = listAdder.divideAndAdd(list.getList(), • list.getLow(), • list.getHigh()); • System.out.println(); • System.out.println("SUM OF LIST..." + total); • System.out.println(); • } • }

  48. The NumberList Class • class NumberList • { • private char [] list; • private int low; • private int high; • public NumberList () • { • int i, noElements; • System.out.print("Enter number of array elements: "); • high = Console.in.readInt(); • Console.in.readChar(); • high = high - 1; • low = 0; • list = new char [high+1]; • defaultInitialization(); • displayList(); • }

  49. The NumberList Class (2) • public void defaultInitialization () • { • int i; • for (i = low; i <= high; i++) • { • if (i % 2 == 0) • list[i] = '1'; • else • list[i] = '+'; • } • }

  50. The NumberList Class (3) • public void displayList () • { • int i; • System.out.println(); • System.out.print("Displaying the number list: "); • System.out.println (list); • System.out.println(); • }

More Related