Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Ruby EBNF COMPSTMT iteration misunderstanding

Here is a basic Ruby program:

puts 1
puts 1
puts 1
puts 1
puts 1
puts 1
puts 1

Here are the top rules of the BNF syntax for Ruby (shortened): source: https://github.com/goruby/goruby/blob/master/goruby.bnf or https://cse.buffalo.edu/~regan/cse305/RubyBNF.pdf

PROGRAM         : COMPSTMT ;

COMPSTMT        : STMT
                | COMPSTMT TERM
                | COMPSTMT TERM EXPR
                | COMPSTMT TERM EXPR TERM ;

STMT            : "BEGIN" "{" COMPSTMT "}"
                | "END" "{" COMPSTMT "}"
                | EXPR ;

Since a PROGRAM is only one COMPSTMT, and a COMPSTMT is never just a {COMPSTMT} , how is this possible for the interpreter to validate my code ? I can't draw the grammar graph leading to my program.

It should only authorize two expressions, no more (COMPSTMT TERM EXPR TERM).

Does Ruby put a begin/end under the hood to authorize an iteration of COMPSTMT, or am I misunderstanding the rules ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Do note that the two BNF you shared differ in some parts. I am going to use the definition from the first source. Also note this is not an official source, this is from the Go language implementation of Ruby, but assume it's correct enough.

First of all we can consider the "puts 1" line as an EXPR as it's a command (see here), so this is what we need to have at the end. However you are right, you cannot get to 7 EXPR values after each other. However that's actually correct, and your issue is that you forgot to add the definition of a TERM value. Let's see what it does:

TERM            : ";"
                | "\n" ;

From this you can easily see that TERM actually means termination (instead of term, a commonly used term (pun intended) in BNF graphs usually occuring when describing the mathematical operation parts of the grammar)

From this it becomes obivous that your code above is not actually 7 EXPR values, it is EXPR "\n" EXPR "\n" EXPR "\n" EXPR "\n" EXPR "\n" EXPR "\n" EXPR, and this we can actually get from the BNF above

Each line is a single substition, except at the very end:

PROGRAM
COMPSTMT
COMPSTMT TERM EXPR
COMPSTMT TERM EXPR TERM EXPR
COMPSTMT TERM EXPR TERM EXPR TERM EXPR
COMPSTMT TERM EXPR TERM EXPR TERM EXPR TERM EXPR
COMPSTMT TERM EXPR TERM EXPR TERM EXPR TERM EXPR TERM EXPR
COMPSTMT TERM EXPR TERM EXPR TERM EXPR TERM EXPR TERM EXPR TERM EXPR
STMT TERM EXPR TERM EXPR TERM EXPR TERM EXPR TERM EXPR TERM EXPR
EXPR TERM EXPR TERM EXPR TERM EXPR TERM EXPR TERM EXPR TERM EXPR
EXPR "\n" EXPR "\n" EXPR "\n" EXPR "\n" EXPR "\n" EXPR "\n" EXPR
"puts 1" "\n" "puts 1" "\n" "puts 1" "\n" "puts 1" "\n" "puts 1" "\n" "puts 1" "\n" "puts 1"
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!