ArticleslgStudy

science

Simple precedence grammar

Simple precedence grammar is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Simple precedence grammar rather than just read about it. In short: In computer science, a simple precedence grammar is a context-free formal grammar that can be parsed with a simple precedence parser. The concept was first created in 1964 by Claude Pair, 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 t…

Key takeaways

  • Simple precedence grammar belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Simple precedence grammar to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Simple precedence grammar from memory before moving on to harder problems.

Reference excerpt

In computer science, a simple precedence grammar is a context-free formal grammar that can be parsed with a simple precedence parser. The concept was first created in 1964 by Claude Pair, 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.

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

There are no erasing rules (ε-productions) There are no useless rules (unreachable symbols or unproductive rules) For each pair of symbols X, Y (X, Y ∈ {\displaystyle \in } (N ∪ Σ)) there is only one Wirth–Weber precedence relation. G is uniquely inversible

Examples

S → a S S b | c {\displaystyle S\to aSSb|c}

precedence table

S a b c $ S = ˙ ⋖ = ˙ ⋖ a = ˙ ⋖ ⋖ b ⋗ ⋗ ⋗ c ⋗ ⋗ ⋗ ⋗ $ ⋖ ⋖ {\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 A simple precedence parser is a type of bottom-up parser for context-free grammars that can be used only by simple precedence grammars. 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 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 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:

Wirth–Weber precedence relationship In computer science, a Wirth–Weber relationship between a pair of symbols ( V t ∪ V n ) {\displaystyle (V_{t}\cup V_{n})} is necessary to determine if a formal grammar is a simple precedence grammar. 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

G = ⟨ V n , V t , S , P ⟩ {\displaystyle G=\langle V_{n},V_{t},S,P\rangle }

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Simple precedence grammar

Start with the simplest possible case. Write down what Simple precedence grammar claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Simple precedence grammar before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Simple precedence grammar ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Simple precedence grammar

In research
Simple precedence grammar appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Simple precedence grammar in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Simple precedence grammar is common in secondary-school and first-year university syllabi. It links to neighbouring topics Formal languages, so understanding it makes those chapters shorter.
In everyday life
Look for Simple precedence grammar outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Simple precedence grammar in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Simple precedence grammar means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Simple precedence grammar out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Simple precedence grammar in simple terms?

In computer science, a simple precedence grammar is a context-free formal grammar that can be parsed with a simple precedence parser. The concept was first created in 1964 by Claude Pair, and was later rediscovered, from ideas due to Robert Floyd, by Niklaus Wirth and Helmut Weber who published a p…

Why does Simple precedence grammar matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Simple precedence grammar?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Simple precedence grammar.

Tags

  • Formal languages

Keep exploring