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

177
Visualizações
PEGjs | Implement Variable in Parser

I am very beginner to PEGjs I need help to implement variable (identifier) declaration support to my parser.

My input code look like:

a=4;
print a

My PEGjs grammer:

start
=(line)*
line
=left:var"="right:integer";" {left=right;}
/ 
print middle:var {return middle;}
print
="print"
var
=(a-zA-z)+
Integer "integer"
= _ [0-9]+ { return parseInt(text(), 10); }

Expected output: 4

Please help me. #ThanksInAdvance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

try this:

all
  = _ mn:multiPutN _ pn:printN _ 
  {
    return mn[pn];
  }

multiPutN
  = mp:putN+ _ 
  {
    var r = {};
    mp.forEach(it => {
        r[it[0]]=it[1];
    });
    return r;
  }
  
putN
  = vn:varName _ "=" _ nn:n _ ";" { return [vn, nn]}

printN
  = print _ n:varName _ {return n;}
  
varName
  = [a-zA-Z]+ {return text();}
  
print 
  ="print"

n "integer number"
  = _ [0-9]+ { return parseInt(text(), 10); }
  
_ "whitespace or new line"
  = [ \t\n\r]*

so that code above also support multi variables but can only print one variable. I wrote the grammar based in your example so when assigning variable value you need to put ";" at the end but print var should not need with that

about 4 years ago · Juan Pablo Isaza 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