VOOZH about

URL: https://en.wikipedia.org/wiki/Simple_precedence_parser

⇱ Simple precedence grammar - Wikipedia


Jump to content
From Wikipedia, the free encyclopedia
(Redirected from Simple precedence parser)
Context-free formal grammar
This article may be too technical for most readers to understand. Please help improve it to make it understandable to non-experts, without removing the technical details. (December 2024) (Learn how and when to remove this message)

In computer science, a simple precedence grammar is a context-free formal grammar that can be parsed with a simple precedence parser.[1] The concept was first created in 1964 by Claude Pair,[2] and was later rediscovered, from ideas due to Robert Floyd, by Niklaus Wirth and Helmut Weber who published a paper, entitled EULER: a generalization of ALGOL, and its formal definition, published in 1966 in the Communications of the ACM.[3]

Formal definition

[edit]

G = (N, Ξ£, P, S) is a simple precedence grammar if all the production rules in P comply with the following constraints:

Examples

[edit]
πŸ‘ {\displaystyle S\to aSSb|c}
precedence table
πŸ‘ {\displaystyle {\begin{array}{c|ccccc}&S&a&b&c&\$\\\hline S&{\dot {=}}&\lessdot &{\dot {=}}&\lessdot &\\a&{\dot {=}}&\lessdot &&\lessdot &\\b&&\gtrdot &&\gtrdot &\gtrdot \\c&&\gtrdot &\gtrdot &\gtrdot &\gtrdot \\\$&&\lessdot &&\lessdot &\end{array}}}

Simple precedence parser

[edit]

A simple precedence parser is a type of bottom-up parser for context-free grammars that can be used only by .

The implementation of the parser is quite similar to the generic bottom-up parser. A stack is used to store a viable prefix of a sentential form from a rightmost derivation. The symbols β‹–, ≐ and β‹— are used to identify the pivot, and to know when to Shift or when to Reduce.

Implementation

[edit]
  • Compute the Wirth–Weber precedence relationship table for a grammar with initial symbol S.
  • Initialize a stack with the starting marker $.
  • Append an ending marker $ to the string being parsed (Input).
  • Until Stack equals "$ S" and Input equals "$"
    • Search the table for the relationship between Top(stack) and NextToken(Input)
    • if the relationship is β‹– or ≐
      • Shift:
      • Push(Stack, relationship)
      • Push(Stack, NextToken(Input))
      • RemoveNextToken(Input)
    • if the relationship is β‹—
      • Reduce:
      • SearchProductionToReduce(Stack)
      • Remove the Pivot from the Stack
      • Search the table for the relationship between the nonterminal from the production and first symbol in the stack (Starting from top)
      • Push(Stack, relationship)
      • Push(Stack, Non terminal)

SearchProductionToReduce (Stack)

  • Find the topmost β‹– in the stack; this and all the symbols above it are the Pivot.
  • Find the production of the grammar which has the Pivot as its right side.

Example

[edit]

Given following language, which can parse arithmetic expressions with the multiplication and addition operations:

E --> E + T' | T'
T' --> T
T --> T * F | F
F --> ( E' ) | num
E' --> E

num is a terminal, and the lexer parse any integer as num; E represents an arithmetic expression, T is a term and F is a factor.

and the Parsing table:

E E' T T' F + * ( ) num $
E ≐ β‹—
E' ≐
T β‹— ≐ β‹— β‹—
T' β‹— β‹— β‹—
F β‹— β‹— β‹— β‹—
+ β‹– ≐ β‹– β‹– β‹–
* ≐ β‹– β‹–
( β‹– ≐ β‹– β‹– β‹– β‹– β‹–
) β‹— β‹— β‹— β‹—
num β‹— β‹— β‹— β‹—
$ β‹– β‹– β‹– β‹– β‹– β‹–
STACK PRECEDENCE INPUT ACTION
$ β‹– 2 * ( 1 + 3 )$ SHIFT
$ β‹– 2 β‹— * ( 1 + 3 )$ REDUCE (F -> num)
$ β‹– F β‹— * ( 1 + 3 )$ REDUCE (T -> F)
$ β‹– T ≐ * ( 1 + 3 )$ SHIFT
$ β‹– T ≐ * β‹– ( 1 + 3 )$ SHIFT
$ β‹– T ≐ * β‹– ( β‹– 1 + 3 )$ SHIFT
$ β‹– T ≐ * β‹– ( β‹– 1 β‹— + 3 )$ REDUCE 4Γ— (F -> num) (T -> F) (T' -> T) (E ->T ')
$ β‹– T ≐ * β‹– ( β‹– E ≐ + 3 )$ SHIFT
$ β‹– T ≐ * β‹– ( β‹– E ≐ + β‹– 3 )$ SHIFT
$ β‹– T ≐ * β‹– ( β‹– E ≐ + < 3 β‹— )$ REDUCE 3Γ— (F -> num) (T -> F) (T' -> T)
$ β‹– T ≐ * β‹– ( β‹– E ≐ + ≐ T β‹— )$ REDUCE 2Γ— (E -> E + T) (E' -> E)
$ β‹– T ≐ * β‹– ( ≐ E' ≐ )$ SHIFT
$ β‹– T ≐ * β‹– ( ≐ E' ≐ ) β‹— $ REDUCE (F -> ( E' ))
$ β‹– T ≐ * ≐ F β‹— $ REDUCE (T -> T * F)
$ β‹– T β‹— $ REDUCE 2Γ— (T' -> T) (E -> T')
$ β‹– E $ ACCEPT

Wirth–Weber precedence relationship

[edit]

In computer science, a Wirth–Weber relationship between a pair of symbols πŸ‘ {\displaystyle (V_{t}\cup V_{n})}
is necessary to determine if a formal grammar is a . In such a case, the simple precedence parser can be used. The relationship is named after computer scientists Niklaus Wirth and Helmut Weber.

The goal is to identify when the viable prefixes have the pivot and must be reduced. A πŸ‘ {\displaystyle \gtrdot }
means that the pivot is found, a πŸ‘ {\displaystyle \lessdot }
means that a potential pivot is starting, and a πŸ‘ {\displaystyle \doteq }
means that a relationship remains in the same pivot.

Formal definition

[edit]
πŸ‘ {\displaystyle G=\langle V_{n},V_{t},S,P\rangle }
πŸ‘ {\displaystyle {\begin{aligned}X\doteq Y&\iff {\begin{cases}A\to \alpha XY\beta \in P\\A\in V_{n}\\\alpha ,\beta \in (V_{n}\cup V_{t})^{*}\\X,Y\in (V_{n}\cup V_{t})\end{cases}}\\X\lessdot Y&\iff {\begin{cases}A\to \alpha XB\beta \in P\\B\Rightarrow ^{+}Y\gamma \\A,B\in V_{n}\\\alpha ,\beta ,\gamma \in (V_{n}\cup V_{t})^{*}\\X,Y\in (V_{n}\cup V_{t})\end{cases}}\\X\gtrdot Y&\iff {\begin{cases}A\to \alpha BY\beta \in P\\B\Rightarrow ^{+}\gamma X\\Y\Rightarrow ^{*}a\delta \\A,B\in V_{n}\\\alpha ,\beta ,\gamma ,\delta \in (V_{n}\cup V_{t})^{*}\\X,Y\in (V_{n}\cup V_{t})\\a\in V_{t}\end{cases}}\end{aligned}}}

Precedence relations computing algorithm

[edit]

We will define three sets for a symbol:

πŸ‘ {\displaystyle {\begin{aligned}\mathrm {Head} ^{+}(X)&=\{Y\mid X\Rightarrow ^{+}Y\alpha \}\\\mathrm {Tail} ^{+}(X)&=\{Y\mid X\Rightarrow ^{+}\alpha Y\}\\\mathrm {Head} ^{*}(X)&=(\mathrm {Head} ^{+}(X)\cup \{X\})\cap V_{t}\end{aligned}}}
Head*(X) is X if X is a terminal, and if X is a non-terminal, Head*(X) is the set with only the terminals belonging to Head+(X). This set is equivalent to First-set or Fi(X) described in LL parser.
Head+(X) and Tail+(X) are βˆ… if X is a terminal.

The pseudocode for computing relations is:

πŸ‘ {\displaystyle \lessdot }
and πŸ‘ {\displaystyle \gtrdot }
are used with sets instead of elements as they were defined, in this case you must add all the cartesian product between the sets/elements.

Example 1

[edit]

πŸ‘ {\displaystyle S\to aSSb|c}

precedence table
πŸ‘ {\displaystyle {\begin{array}{c|ccccc}&S&a&b&c&\$\\\hline S&\doteq &\lessdot &\doteq &\lessdot &\\a&\doteq &\lessdot &&\lessdot &\\b&&\gtrdot &\gtrdot &\gtrdot &\gtrdot \\c&&\gtrdot &\gtrdot &\gtrdot &\gtrdot \\\$&&\lessdot &&\lessdot &\end{array}}}

Example 2

[edit]

πŸ‘ {\displaystyle S\to a|aT|[S]}

πŸ‘ {\displaystyle T\to b|bT}

  • Head+( S ) = { a, [ }
  • Head+( a ) = βˆ…
  • Head+( T ) = { b }
  • Head+( [ ) = βˆ…
  • Head+( ] ) = βˆ…
  • Head+( b ) = βˆ…
  • Tail+( S ) = { a, T, ], b }
  • Tail+( a ) = βˆ…
  • Tail+( T ) = { b, T }
  • Tail+( [ ) = βˆ…
  • Tail+( ] ) = βˆ…
  • Tail+( b ) = βˆ…
  • Head*( S ) = { a, [ }
  • Head*( a ) = a
  • Head*( T ) = { b }
  • Head*( [ ) = [
  • Head*( ] ) = ]
  • Head*( b ) = b
precedence table
πŸ‘ {\displaystyle {\begin{array}{c|ccccccc}&S&T&a&b&[&]&\$\\\hline S&&&&&&\doteq &\doteq \\T&&&&&&\gtrdot &\gtrdot \\a&&\doteq &&\lessdot &&\gtrdot &\gtrdot \\b&&\doteq &&\lessdot &&\gtrdot &\gtrdot \\{\text{[}}&\doteq &&\lessdot &&\lessdot &&\\]&&&&&&\gtrdot &\gtrdot \\\$&\doteq &&\lessdot &&\lessdot &&\end{array}}}

Notes

[edit]
  1. ^ The Theory of Parsing, Translation, and Compiling: Compiling, Alfred V. Aho, Jeffrey D. Ullman, Prentice–Hall, 1972.
  2. ^ Claude Pair (1964). "Arbres, piles et compilation". Revue franΓ§aise de traitement de l'information., in English Trees, stacks and compiling
  3. ^ Machines, Languages, and Computation, Prentice–Hall, 1978, ISBN 9780135422588, Wirth and Weber [1966] generalized Floyd's precedence grammars, obtaining the simple precedence grammars.

References

[edit]
  • Alfred V. Aho, Jeffrey D. Ullman (1977). Principles of Compiler Design. 1st Edition. Addison–Wesley.
  • William A. Barrett, John D. Couch (1979). Compiler construction: Theory and Practice. Science Research Associate.
  • Jean-Paul Tremblay, P. G. Sorenson (1985). The Theory and Practice of Compiler Writing. McGraw–Hill.

Further reading

[edit]
  • Aho, Alfred V.; Ullman, Jeffrey D., The theory of parsing, translation, and compiling

External links

[edit]