LANGUAGE TRANSLATORS: WEEK 3 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

LANGUAGE TRANSLATORS: WEEK 3

Description:

PARSING ALGORITHMS invariably are based on the CONTEXT FREE GRAMMAR that defines ... and outputs a PARSE TREE (in the form of Java Constructors) ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 14
Provided by: Computing115
Category:

less

Transcript and Presenter's Notes

Title: LANGUAGE TRANSLATORS: WEEK 3


1
LANGUAGE TRANSLATORS WEEK 3
  • LECTURE
  • Grammar Theory
  • Introduction to Parsing
  • Parser - Generators
  • PRACTICAL/TUTORIAL
  • JAVACUP Questions on grammar theory
  • WEEKLY WORK Read Chapter 1 of Appels Book, read
    the JavaCup manual (both are online)

2
GRAMMARS
  • Theoretical properties and results about the
    nature of GRAMMARS have been used for 40 years to
    influence the design of PARSERS.
  • A CONTEXT FREE (BNF) GRAMMAR contains a
    vocabulary terminals, non-terminals and
    productions of the form
  • non-terminal v1 v2 .........vn
  • where the vi are members of the vocabulary.
  • One non-terminal is called the SPECIAL SYMBOL

3
PROPERTIES OF GRAMMARS -1
  • A syntax tree of a BNF Grammar G is a tree where
  • - the root is the special symbol of G,
  • - the leaves are terminals of G,
  • - the nodes are non-terminals of G,
  • - each node is the LHS of some production P of
    G in which the nodes children form the RHS of
    P

4
PROPERTIES OF GRAMMARS -2
  • A string is in the language generated by a BNF
    grammar G IFF it forms the leaves (in the correct
    order) of some syntax tree generated by G
  • G is AMBIGUOUS IFF
  • at least one of the strings in its language has
    more
  • than one distinct syntax tree

5
PROPERTIES OF GRAMMARS -3
  • A BNF grammar G is LEFT-RECURSIVE if has at least
    one production of the form
  • X X w
  • where X is a non-terminal and w is a string of
    symbols in Gs vocabulary

6
PROPERTIES OF GRAMMARS -4
  • If w is a list of symbols in the vocabulary of
    BNF grammar G then
  • First(w) set of TERMINAL symbols that may be at
    the front of ANY string derived from w using Gs
    productions

7
PROPERTIES OF GRAMMARS -5
  • If X is a non-terminal of BNF grammar G then
  • Follow(X) set of TERMINAL symbols that can
    follow X in a derivation from Gs special symbol
    using Gs productions.
  • Nullable(X) is true IFF the empty word can be
    derived from X

8
PARSING
  • A PARSER (or PARSING ALGORITHM) derives
    SYNTAX/PARSE TREES from a sequence of TOKENS
  • Some well-known tools are little more than
    PARSERS e.g. Syntax Directed Editors - those that
    colour different syntax classes or point out
    syntax errors as you make them
  • PARSING ALGORITHMS invariably are based on the
    CONTEXT FREE GRAMMAR that defines the language
    being parsed.

9
Example
  • Input String b 5 a 5 b
    PRINT(b a)
  • Ouput PARSE TREE (as a Java data structure)
  • new CompoundStm(
  • new AssignStm("b",new NumExp(5)),
  • new CompoundStm(
  • new AssignStm("a", new OpExp(new
    NumExp(5),
  • OpExp.Times,
  • new
    IdExp("b"))),
  • new PrintStm(new LastExpList(new
    OpExp(new IdExp("b"),
  • OpExp.Times, new
    IdExp("a"))))
  • )
  • )

10
PARSING ALGORITHMS
  • Parsers that follow the languages grammar can be
  • TOP DOWN - starting with the grammars special
    symbol, try to find a derivation of the string of
    tokens being parsed, consuming one token at a
    time
  • BOTTOM UP - start with the string viewed as a
    stack. Match the top n tokens of the stack with
    the RHS of a grammar rule P and replace those
    tokens with the LHS of P

11
PRACTICAL EXAMPLE OF THEORY
  • A LL(1) PARSING TABLE (-like a program) can be
    automatically derived from a grammar G if and
    only if
  • (i) G is NOT ambiguous
  • (ii) G is NOT left recursive
  • (iii) for EVERY two of Gs productions of the
    form
  • X W1, X W2, it is the case that
    First(W1) and First(W2) have no common element

12
Practical JavaCup
  • JavaCup is a tool that inputs a GRAMMAR in BNF
    form ( other stuff..) and outputs a PARSE TREE
    (in the form of Java Constructors).
  • Parsers created using JavaCup accept a sequence
    of TOKENS as input from a SCANNER such as last
    weeks.
  • An easy way to show JavaCups use is to implement
    a simple interpreter with it
  • (i) JavaCup inputs a grammar and creates a
    corresponding parser P in java.
  • (ii) P accepts tokens from the scanned input and
    generates and passes the parse tree to some Java
    code which then EVALUATES it.

13
SUMMARY
  • Parsers check strings are legal according to
    grammatical definitions, and build up a structure
    representing legal strings (parse trees)
  • Parsers can sometimes be auto-generated from the
    defining grammar
  • Theory of grammars helps us in the
    auto-generation of parsers
Write a Comment
User Comments (0)
About PowerShow.com