Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Simple precedence grammar

From Wikipedia, the free encyclopedia
(Redirected fromSimple precedence parser)
Context-free formal grammar
This articlemay be too technical for most readers to understand. Pleasehelp improve it tomake it understandable to non-experts, without removing the technical details.(December 2024) (Learn how and when to remove this message)

Incomputer science, asimple precedence grammar is acontext-freeformal grammar that can be parsed with asimple precedence parser.[1] The concept was first created in 1964 byClaude Pair,[2] and was later rediscovered, from ideas due toRobert Floyd, byNiklaus Wirth andHelmut Weber who published a paper, entitledEULER: a generalization of ALGOL, and its formal definition, published in 1966 in theCommunications of the ACM.[3]

Formal definition

[edit]

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

Examples

[edit]
SaSSb|c{\displaystyle S\to aSSb|c}
precedence table
Sabc$S=˙=˙a=˙bc${\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]

Asimple precedence parser is a type ofbottom-up parser forcontext-free grammars that can be used only bysimple precedence grammars.

The implementation of the parser is quite similar to the genericbottom-up parser. A stack is used to store aviable prefix of asentential form from arightmost derivation. The symbols ⋖, ≐ and ⋗ are used to identify thepivot, and to know when toShift or when toReduce.

Implementation

[edit]
  • Compute theWirth–Weber precedence relationship table for a grammar with initial symbol S.
  • Initialize a stack with thestarting marker $.
  • Append anending 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 thePivot.
  • Find the production of the grammar which has thePivot 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' --> TT  --> T * F  | FF  --> ( E' ) | numE' --> E

num is a terminal, and thelexer parse any integer asnum;E represents an arithmetic expression,T is a term andF is a factor.

and the Parsing table:

EE'TT'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]

Incomputer science, aWirth–Weber relationship between a pair of symbols(VtVn){\displaystyle (V_{t}\cup V_{n})} is necessary to determine if aformal grammar is asimple precedence grammar. In such a case, thesimple precedence parser can be used. The relationship is named after computer scientistsNiklaus Wirth and Helmut Weber.

The goal is to identify when the viable prefixes have thepivot and must be reduced. A{\displaystyle \gtrdot } means that thepivot is found, a{\displaystyle \lessdot } means that a potentialpivot is starting, and a{\displaystyle \doteq } means that a relationship remains in the samepivot.

Formal definition

[edit]
G=Vn,Vt,S,P{\displaystyle G=\langle V_{n},V_{t},S,P\rangle }
XY{AαXYβPAVnα,β(VnVt)X,Y(VnVt)XY{AαXBβPB+YγA,BVnα,β,γ(VnVt)X,Y(VnVt)XY{AαBYβPB+γXYaδA,BVnα,β,γ,δ(VnVt)X,Y(VnVt)aVt{\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:

Head+(X)={YX+Yα}Tail+(X)={YX+αY}Head(X)=(Head+(X){X})Vt{\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) isX ifX is a terminal, and ifX is a non-terminal, Head*(X) is the set with only the terminals belonging to Head+(X). This set is equivalent toFirst-set orFi(X) described inLL parser.
Head+(X) and Tail+(X) are ∅ ifX 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 thecartesian product between the sets/elements.

Example 1

[edit]

SaSSb|c{\displaystyle S\to aSSb|c}

precedence table
Sabc$Sabc${\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]

Sa|aT|[S]{\displaystyle S\to a|aT|[S]}

Tb|bT{\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
STab[]$STab[]${\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 EnglishTrees, 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]
Top-down
Bottom-up
Mixed, other
Related topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Simple_precedence_grammar&oldid=1316999801#Simple_precedence_parser"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp