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 9169327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:51:25+00:00 2026-06-17T15:51:25+00:00

I’m new to functional programming and was wondering how one solves the problem of

  • 0

I’m new to functional programming and was wondering how one solves the problem of calculating the set of nullable nonterminals in a context-free grammar in a pure functional way without using variable assignments.

A nullable nonterminal is a nonterminal directly yielding empty, e.g., A ::= , or
having a body containing of nullable nonterminals, e.g., A ::= B C D, where all B C and D yield empty.

I’m using the following definitions in Scala to define a grammar:

case class Grammar(name:String, startSymbol:Nonterminal, rules:List[Rule])
case class Rule(head: Nonterminal, body:List[Symbol])
abstract class Symbol
case class Terminal(c:Char) extends Symbol
case class Nonterminal(name:String) extends Symbol

A basic algorithm is that to gather all directly nullable nonterminals and put them in a set.
Then in each iteration try to determine which production rules have all nullable nonterminals
on their body. Those nonterminals will be added to the set until no new nonterminal is added to the
set.

I have implemented this procedure in Scala as:

  def getNullableNonterminals(grammar:Grammar) = {

  var nieuw : Set[Nonterminal] = (for(Rule(head, Nil) <- grammar.rules) yield head) (collection.breakOut)
  var old = Set[Nonterminal]()

  while(old != nieuw) {
    old = nieuw
    for{
        Rule(head, symbols) <- grammar.rules
        if symbols.length > 0
        if symbols.forall( s => s.isInstanceOf[Nonterminal] && old.contains(s.asInstanceOf[Nonterminal]))
       } nieuw = nieuw + head
  }
  nieuw   
}

Although the code is much shorter than the equivalent Java version, it uses variables. Any suggestions
to rewrite this piece of code in a functional style?

  • 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-06-17T15:51:26+00:00Added an answer on June 17, 2026 at 3:51 pm

    Here is a more idiomatic Scala solution:

    object Algorithm {
    
      def getNullableNonterminals(grammar:Grammar) = {
        loop(grammar, Set())
      }
    
      @tailrec
      private def loop(grammar: Grammar, nullablesSoFar: Set[Nonterminal]): Set[Nonterminal] = {
        val newNullables = generateNew(grammar, nullablesSoFar)
        if (newNullables.isEmpty)
          nullablesSoFar //no new nullables found, so we just return the ones we have
        else
          loop(grammar, nullablesSoFar ++ newNullables) //add the newly found nullables to the solution set and we keep going
      }
    
      private def generateNew(grammar: Grammar, nullableSoFar: Set[Nonterminal]) = {
        for {
          Rule(head, body) <- grammar.rules
          if !nullableSoFar.contains(head)
          if body.forall(isNullable(_, nullableSoFar))
        } yield head
      }
    
      //checks if the symbol is nullable given the current set of nullables
      private def isNullable(symbol: Symbol, provenNullable: Set[Nonterminal]) = symbol match {
        case Terminal(_) => false
        case x@Nonterminal(_) => provenNullable.contains(x)
      }
    
    }
    

    The while statement is replaced with a recursive function – loop.
    Also, avoid using isInstanceOf – pattern matching is much better suited for this.

    Small observation – make the Symbol class sealed, since this can enforce warnings of missing cases in pattern matches.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I'm making a simple page using Google Maps API 3. My first. One marker
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.