Theoretical Concepts For The Parsing Assignment - PowerPoint PPT Presentation

1 / 58
About This Presentation
Title:

Theoretical Concepts For The Parsing Assignment

Description:

Jack and Jill or Bob and Bill went up the hill. or. Jack and Jill. Bob and Bill went up the hill. ... Introduced by Jim Backus and developed by Pete Naur to ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 59
Provided by: jame1
Category:

less

Transcript and Presenter's Notes

Title: Theoretical Concepts For The Parsing Assignment


1
Theoretical Concepts For The Parsing Assignment
A return to the compilation process Parsing and
Formal grammars Divide and conquer through
recursion
2
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 lt 5) statement statement

while (i lt 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
A Perspective Into Assignment 4
Perform a syntactical analysis
.
9
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
  • ltAgt ltB1gt ltB2gt ltB3gtltBngt
  • Example two (alternatives)
  • ltAgt ltB1gt ltB2gt ltB3gt ltB2gt ltB4gt ltB1gt
  • Example three (program specification)
  • x x 1
  • ltAssignment statementgt ltvariablegt lt gt
    ltexpressiongt

12
BNF Assignment 4
  • (The following specifications come from the main
    233 course web page www.cpsc.ucalgary.ca/becker/
    233)
  • ltSTATEMENTgt ltSentencegt ltPUNCTgt
  • ltSentencegt ltNounPhrasegt ltVerbPhrasegt 
  • ltNounPhrasegt ltVerbPhrasegt ltConjunctiongt
    ltSentencegt
  • ltNounPhrasegt ltProNoungt ltProperNoungt
    ltArticlegt ltAdjectiveListgt ltNoungt ltArticlegt
    ltNoungt ltNoungt

13
BNF Assignment 4
  • ltVerbPhrasegt ltAdverbListgt ltVerbgt ltVerbgt
    ltAdverbListgt
  • ltVerbgt ltNounPhrasegt ltAdverbListgt
  • ltAdverbListgt ltVerbgt ltNounPhrasegt
  • ltVerbgt
  • ltAdjectiveListgt ltAdjectiveListgt ltAdjectivegt
    ltnothinggt
  • ltAdverbListgt ltAdverbListgt ltConjunctiongt
    ltAdverbgt ltAdverbgt

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

ltgt
ltgt
15
Syntax Diagrams General Examples
  • Example one
  • Example two

i 1, 2, 3n
16
Syntax Diagrams General Examples (2)
  • Example three (program specification)
  • x x 1

ltAssignment statementgt
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
1
2

34
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

5
6
7
8
35
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

5
6
7
8
1
36
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

5
6
7
8
1
5
37
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

5
6
7
8
1
0
38
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

5
6
7
8
1
7
8
39
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

5
6
7
8
1
1
40
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

5
6
7
8
1

1
41
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

2
42
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

2

43
Divide And Conquer An Example
0
1
2
3
4
5
6
7
8
1
2

2

44
Divide And Conquer An Example
Final result after recursive calls 5
45
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.ge
    tList(),

  • 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 high1
  • defaultInitialization()
  • displayList()

49
The NumberList Class (2)
  • public void defaultInitialization ()
  • int i
  • for (i low i lt high i)
  • if (i 2 0)
  • listi '1'
  • else
  • listi ''

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()

51
Class Adder AsciiToInteger
  • class Adder
  • private int asciiToInteger (char ch)
  • int temp
  • // Recall that the ascii value for the
    character '0' is 48.
  • temp (int) (ch - 48)
  • return temp

52
Class Adder DivideAndAdd
  • class Adder
  • public int divideAndAdd (char array, int
    low, int high)
  • System.out.println("SUBDIVIDED ARRAY "
    "low" low " " "high" high)

53
Class Adder DivideAndAdd (2)
  • // THREE BASE CASES
  • // One element in sublist convert from
    char to int and return if it's a number.
  • if (low high)
  • if (arraylow ! '')
  • int temp asciiToInteger(arraylo
    w)
  • return temp
  • else
  • // It's a plus sign don't sum the
    ascii value.
  • return(0)

54
Class Adder DivideAndAdd (3)
  • // Two elements in sublist
  • if ((low1)high)
  • // Order of elements operation,
    operand
  • if (arraylow '')
  • int temp asciiToInteger
    (arrayhigh)
  • return temp
  • // Order of elements operand,
    operation
  • else if (arrayhigh '')
  • int temp asciiToInteger
    (arraylow)
  • return temp

55
Class Adder DivideAndAdd (4)
  • // Three elements in sublist
  • if ((low2) high)
  • // Order of elements ltoperandgt
    ltoperationgt ltoperandgt
  • if (arraylow ! '')
  • int operand1 asciiToInteger(arra
    ylow)
  • int operand2 asciiToInteger(arra
    yhigh)
  • return (operand1operand2)
  • // Order of elements ltoperationgt
    ltoperandgt ltoperationgt
  • else
  • int temp asciiToInteger(arraylo
    w1)
  • return temp

56
Class Adder DivideAndAdd (5)
  • // RECURSIVE CASES
  • // More than four elements in the list.
  • int middle, leftTotal, rightTotal, total
  • int leftLow, leftHigh, rightLow,
    rightHigh
  • middle (int) ((lowhigh)/2)
  • // Set low and high bound for the left
    sublist.
  • leftLow low
  • leftHigh middle - 1
  • // Set low and high bound for the right
    sublist.
  • rightLow middle 1
  • rightHigh high
  • leftTotal divideAndAdd(array, leftLow,
    leftHigh)
  • rightTotal divideAndAdd(array,
    rightLow, rightHigh)

57
Class Adder DivideAndAdd (7)
  • total leftTotal rightTotal
  • if (arraymiddle ! '')
  • total total asciiToInteger(arraym
    iddle)
  • // Recursive calls finished.
  • return total

58
Summary
  • You should now know
  • Compilation What are the major parts of a
    compiler
  • How formal grammars can be used to specify the
    syntax of a language
  • Two examples of specifying syntax rules
  • Backus-Naur form (BNF)
  • Syntax diagrams
  • Divide and Conquer through recursion
Write a Comment
User Comments (0)
About PowerShow.com