سؤال

Suppose I have the following fictional grammar, with a recursive definition for clause.

sentence := clause +
subject := (qualifier *) subjectiveNoun
objects := object +
object := nothing | (qualifier *) objectiveNoun
clause := subject objects verb
qualifier := adjective | clause

Assuming everything else has a non recursive definition, how should can approach writing a javascript grammar for 'clause'? If someone is familiar with the XRegExp library to do this, that would be an added plus.

هل كانت مفيدة؟

المحلول

The XRegExp library does support recursive matching1, but what is your goal here? If you're able to match this with regex, then what? If you're planning to process the parse tree, then regex is of little use, this will only tell you the source matched or not. In that case, have a look into PEG.js or Jison.

1 http://xregexp.com/plugins/#matchRecursive

نصائح أخرى

Ometa makes it easy to write parsers in JS for analytic grammars and does some tricks to handle LR which means it should be able to handle your grammar, but PEG grammars are rarely used for natural language parsing.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top