Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6926823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:58:05+00:00 2026-05-27T10:58:05+00:00

Given a simple grammar, like rule1 := token1 token2 token3 token4 || token1 token2

  • 0

Given a simple grammar, like

rule1
    := token1 token2 token3 token4
    || token1 token2 token3 token3;

What’s the difference between shifting the first three tokens, then looking at the fourth to see which rule to reduce, and simply performing a lookahead of three tokens to see which rule to reduce?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:58:06+00:00Added an answer on May 27, 2026 at 10:58 am

    In a shift/reduce parser, the lookahead is not used to determine which production is being considered, but rather to decide whether the parser should shift the next token or take some sort of reduce action. If you had a shift/reduce parser for the above grammar, the parser would always shift four tokens before deciding whether or not to reduce; remember that in LR parsers, reductions only are performed when the appropriate series of symbols is atop the parsing stack. Lookahead would only be necessary here if the parser couldn’t tell whether it should reduce the four tokens it had or to keep shifting more symbols and reduce later on.

    Specifically, the parser would probably do something like this:

    Stack                           Input                        Action
    -------------------------------------------------------------------------------
                                    token1 token2 token3 token4  Shift
    token1                                 token2 token3 token4  Shift
    token1 token2                                 token3 token4  Shift
    token1 token2 token3                                 token4  Shift
    token1 token2 token3 token4                                  Reduce, Option 1
    rule1                                                        Accept
    

    Or

    Stack                           Input                        Action
    -------------------------------------------------------------------------------
                                    token1 token2 token3 token3  Shift
    token1                                 token2 token3 token3  Shift
    token1 token2                                 token3 token3  Shift
    token1 token2 token3                                 token3  Shift
    token1 token2 token3 token3                                  Reduce, Option 2
    rule1                                                        Accept
    

    Note that this contrasts with top-down parsers like LL(k) parsers, which work by trying to predict the production to use. In that case, four lookahead tokens would be required, because the parser is guessing the production and then checking its guess (predict/match parsing). For example, in a top-down parser (which would have to be LL(4) here), it would do the following:

    Stack                           Input                             Action
    ----------------------------------------------------------------------------------
    rule1                           token1 token2 token3 token4 $$$$  Predict, Option 1
    token1 token2 token3 token4     token1 token2 token3 token4 $$$$  Match
    token2 token3 token4            token2 token3 token4 $$$$         Match
    token3 token4                   token3 token4 $$$$                Match
    token4                          token4 $$$$                       Match
                                    $$$$                              Accept
    

    Or

    Stack                           Input                             Action
    ----------------------------------------------------------------------------------
    rule1                           token1 token2 token3 token3 $$$$  Predict, Option 2
    token1 token2 token3 token3     token1 token2 token3 token3 $$$$  Match
    token2 token3 token3            token2 token3 token3 $$$$         Match
    token3 token3                   token3 token3 $$$$                Match
    token3                          token3 $$$$                       Match
                                    $$$$                              Accept
    

    Notice how the lookahead is needed to predict which production to use, so the parser must have four tokens of lookahead. In an LR parser, the parser works by inspecting more tokens until it’s comfortable that it’s seen what it’s looking for, then reduces (shift/reduce parsing). In this case, no lookahead is required at all. Lookahead is only required in an LR parser to determine whether the parser has seen the end of the handle (the string to reduce), or whether it’s in the middle of the handle and has to keep shifting. This is why, for example, some interesting grammars can be shown to be LR(0), but the only grammars that are LL(0) are grammars in which each nonterminal has exactly one production associated with it; the lookahead has fundamentally different uses in top-down versus bottom-up parsing.

    Generally speaking, top-down parsers can handle fewer grammars than bottom-up parsers, and in fact any LL(k) grammar is guaranteed to be LR(k) but not the other way around.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a simple C# class definition like: [System.Windows.Markup.ContentProperty(PropertyOne)] public class SimpleBase { public string
Given a simple data set, I would like to be able to calculate a
This is a purely theoretical question. Given three simple classes: class Base { }
Given a simple Parsekit grammar. @start = sentence+; sentence = 'beer' container; container =
Given a simple Java class like this: class MyData { public int a; public
Given a simple node, I'd like to handle click event on a node. For
Given a simple Silverlight Grid like this: <Grid ShowGridLines=True> <Grid.ColumnDefinitions> <ColumnDefinition x:Name=MyColumn Width=2* />
Given a simple statement, such as: <statement id=SelectProducts resultMap=???> SELECT * FROM Products </statement>
Given a simple (id, description) table t1, such as id description -- ----------- 1
Given a simple switch statement switch (int) { case 1 : { printf(1\n); break;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.