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

  • Home
  • SEARCH
  • 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 626811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:27:34+00:00 2026-05-13T19:27:34+00:00

Given the following grammar: S -> L=L s -> L L -> *L L

  • 0

Given the following grammar:

S -> L=L  
s -> L  
L -> *L  
L -> id  

What are the first and follow for the non-terminals?

If the grammar is changed into:

S -> L=R  
S -> R  
L -> *R  
L -> id  
R -> L  

What will be the first and follow ?

  • 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-13T19:27:34+00:00Added an answer on May 13, 2026 at 7:27 pm

    When I took a compiler course in college I didn’t understand FIRST and FOLLOWS at all. I implemented the algorithms described in the Dragon book, but I had no clue what was going on. I think I do now.

    I assume you have some book that gives a formal definition of these two sets, and the book is completely incomprehensible. I’ll try to give an informal description of them, and hopefully that will help you make sense of what’s in your book.

    The FIRST set is the set of terminals you could possibly see as the first part of the expansion of a non-terminal. The FOLLOWS set is the set of terminals you could possibly see following the expansion of a non-terminal.

    In your first grammar, there are only three kinds of terminals: =, *, and id. (You might also consider $, the end-of-input symbol, to be a terminal.) The only non-terminals are S (a statement) and L (an Lvalue — a “thing” you can assign to).

    Think of FIRST(S) as the set of non-terminals that could possibly start a statement. Intuitively, you know you do not start a statement with =. So you wouldn’t expect that to show up in FIRST(S).

    So how does a statement start? There are two production rules that define what an S looks like, and they both start with L. So to figure out what’s in FIRST(S), you really have to look at what’s in FIRST(L). There are two production rules that define what an Lvalue looks like: it either starts with a * or with an id. So FIRST(S) = FIRST(L) = { *, id }.

    FOLLOWS(S) is easy. Nothing follows S because it is the start symbol. So the only thing in FOLLOWS(S) is $, the end-of-input symbol.

    FOLLOWS(L) is a little trickier. You have to look at every production rule where L appears, and see what comes after it. In the first rule, you see that = may follow L. So = is in FOLLOWS(L). But you also notice in that rule that there is another L at the end of the production rule. So another thing that could follow L is anything that could follow that production. We already figured out that the only thing that can follow the S production is the end-of-input. So FOLLOWS(L) = { =, $ }. (If you look at the other production rules, L always appears at the end of them, so you just get $ from those.)

    Take a look at this Easy Explanation, and for now ignore all the stuff about ϵ, because you don’t have any productions which contain the empty-string. Under “Rules for First Sets”, rules #1, #3, and #4.1 should make sense. Under “Rules for Follows Sets”, rules #1, #2, and #3 should make sense.

    Things get more complicated when you have ϵ in your production rules. Suppose you have something like this:

    D -> S C T id = V  // Declaration is [Static] [Const] Type id = Value
    S -> static | ϵ    // The 'static' keyword is optional
    C -> const | ϵ     // The 'const' keyword is optional
    T -> int | float   // The Type is mandatory and is either 'int' or 'float'
    V -> ...           // The Value gets complicated, not important here.
    

    Now if you want to compute FIRST(D) you can’t just look at FIRST(S), because S may be “empty”. You know intuitively that FIRST(D) is { static, const, int, float }. That intuition is codified in rule #4.2. Think of SCT in this example as Y1Y2Y3 in the “Easy Explanation” rules.

    If you want to compute FOLLOWS(S), you can’t just look at FIRST(C), because that may be empty, so you also have to look at FIRST(T). So FOLLOWS(S) = { const, int, float }. You get that by applying “Rules for follow sets” #2 and #4 (more or less).

    I hope that helps and that you can figure out FIRST and FOLLOWS for the second grammar on your own.

    If it helps, R represents an Rvalue — a “thing” you can’t assign to, such as a constant or a literal. An Lvalue can also act as an Rvalue (but not the other way around).

    a = 2;  // a is an lvalue, 2 is an rvalue
    a = b;  // a is an lvalue, b is an lvalue, but in this context it's an rvalue
    2 = a;  // invalid because 2 cannot be an lvalue
    2 = 3;  // invalid, same reason.
    *4 = b; // Valid!  You would almost never write code like this, but it is
            // grammatically correct: dereferencing an Rvalue gives you an Lvalue.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following which can be loaded into MS SAPI 5.1: <GRAMMAR LANGID=409> <RULE
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>
I have the following ANTLR grammar which is given as an example by Terrence
Given the following basic grammar I want to understand how I can handle comment
Given the following EBNF grammar (found on wikipedia for PL/0), what is an expression
The ANSI C grammar from -link- give me the following rules for array declarations:
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected

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.