lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I'm trying describe EBNF parser with lpeg library but my implementation of parser don't recognize some sentences. 
The grammar defined by me is show below:

local function t(s) return (P(s) * Space1) end
-- Lexical Elements
local Space = S(" \n\t")^0
local idSafe = R('AZ', 'az')
local DecimalDigit = R('09')
local MetaID = idSafe * (idSafe + P('_') + DecimalDigit)^0 * Space
local string = (P('"') * (P('\\') * P(1) + (1 - P('"')))^0 * P('"') +
               P("'") * (P("\\")* P(1) + (1 - P("'")))^0 * P("'")) * Space

local SyntaxRule, Def, SingleDef = V('SyntaxRule'), V('Def'), V('SingleDef')
local Term, Primary = V('Term'), V('Primary')
local OptionalSequence, RepeatedSequence, RepeatedSequenceP, GroupedSequence,
    TerminalString, Empty = V('OptionalSequence'), V('RepeatedSequence'), V('RepeatedSequenceP'), V('GroupedSequence'),
    V('TerminalString'), V('Empty')

local G4 = P{"Syntax";
  Syntax = SyntaxRule^1,
  SyntaxRule = MetaID * t('=') * Def * t(';'),
  Def = SingleDef * (t('|') * SingleDef)^0,
  SingleDef = Term * (t(',') * Term)^0,
  Term = Primary,
  Primary = TerminalString +  
    OptionalSequence +
    RepeatedSequence +
    GroupedSequence +
    MetaID +
    Empty,
  TerminalString = string,
  Empty = Space,
  OptionalSequence = t('[') * Def * t(']'),
  RepeatedSequence = t('{') * Def * t('}') * RepeatedSequenceP + --ZeroOrMore 
    MetaID * t('+') * RepeatedSequenceP +
    string * t('+') * RepeatedSequenceP +  
    t('[') * Def * t(']') * t('+') * RepeatedSequenceP +    
    GroupedSequence * t('+') * RepeatedSequenceP +
    Space * t('+') * RepeatedSequenceP + --OneOrMore
    
    MetaID * t('+') * TerminalString * RepeatedSequenceP +
    string * t('+') * TerminalString * RepeatedSequenceP +  
    t('[') * Def * t(']') * t('+') * TerminalString * RepeatedSequenceP +    
    GroupedSequence * t('+') * TerminalString * RepeatedSequenceP +
    Space * t('+') * TerminalString * RepeatedSequenceP, --OneOrMoreSeparation
    
  RepeatedSequenceP =  t('') +
    t('+') * RepeatedSequenceP + 
    t('+') * TerminalString * RepeatedSequenceP,
    
  GroupedSequence = t('(') * Def * t(')')
}

G4 = G4 * -1 / '%0'

But with this grammar, I don't recognize the sentence "A = 'a'+;". The code I use for this is 

print(lpeg.match(G4, "A = 'a'+;"))

Any idea why this problem happens ?

Thanks in advice,
Cleverton Hentz