How can I parse a list of name = value pairs in a parser generator (ANTLR, YACC, etc.)?

I want to parse a list of pairs (separated by spaces) in the form

name1=value1 name2=value2 ...

      

Where:

  • NAME can contain anything except spaces and an equal sign
  • VALUE can contain anything except spaces (including equal signs!)

The problem is that the parser matches the input like

name1=value1

      

as separate tokens 'NAME EQUALS VALUE'

, not as a single token 'VALUE'

.

PS. I know this is trivial to code directly, but I need it in the context of a stronger parser.

+1


a source to share


3 answers


Here's what's in antlr that parses this:

a=b=c=d c=d e=f

      



This may not be all you need, but it should be the core.

grammar NameValuePairs;

pairs   :  namevaluepair (WS namevaluepair)*;

namevaluepair
  :  name '=' value;

name  :  ID;

value  :  ID ('=' ID)*;

WS  :  ' ' {skip()};

EQ  :  '=';

ID  :  ~(' ' | '=')*;

      

+1


a source


I think you may run into a problem if VALUE can contain an equal sign. I think it would be better, if possible, to make the equal sign a reserved character or switch to another reserved character to denote "=".



I'm not sure if this will work in the context of your stronger parser, but you can split the space by giving you an array (or whatever data structure your language uses) of "NAME = VALUE" pairs. Then loop through the array and strip again the reserved character you are using for '='. If you cannot change or reserve '=', you can specify a regular expression for the first instance of '='. I hope I'm not from here!

0


a source


You don't need a strong parser for name value pairs sufficient for a regular expression. If you don't have a contextual or nested structure, this "job" belongs to the lexer, not the parser :)

0


a source







All Articles