[antlr-interest] Freeform Text Parsing

Sam Harwell sharwell at pixelminegames.com
Tue Dec 21 06:35:44 PST 2010


You need to include the following rule at the end of your lexer. Without
wildcards, all allowed characters must appear in explicit rules.

ANY_CHAR : . ;

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of John Rossi
Sent: Tuesday, December 21, 2010 8:26 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Freeform Text Parsing


I plan to use ANTLR to parse generated (and hence predictable) English
sentences.  To verify that I know what I'm doing, I wanted to create a
grammar that parses a simple address book into an entry tree.

The following grammar is wrong, but it expresses my intent.  (~(NEWLINE))+
doesn't grab arbitrary non-newline text, but rather matches known,
non-newline tokens, which isn't what I want.  What's the right way?  Or is
ANTLR unsuitable for grammars that can't identify string literals at the
lexing stage?

John Rossi
Home
555-7293

Michael Raster
Work
555-8374

grammar AddressBook;
options {
output=AST;
ASTLabelType=CommonTree;
}

tokens {
ENTRIES;
ENTRY;
NAME;
CONTACTTYPE;
PHONE;
HOME;
WORK;
}

@header {
package org.roxton;
}

@lexer::header {
package org.roxton;
}

addressbook
:(entry (NEWLINE)?)+ -> ^(ENTRIES entry+); entry:name NEWLINE contactType
NEWLINE phone NEWLINE -> ^(ENTRY ^(NAME name) ^(CONTACTTYPE contactType)
^(PHONE phone)); name:(~(NEWLINE))+; contactType :('Home'->HOME |
'Work'->WORK); phone:(~(NEWLINE))+;

NEWLINE:'\r'? '\n' ;



      

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list