Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda