I'd like to define a grammar for a simple language.
The language allows some sort of assignments.
Example
keyworda: this is the 1 keyword-A
keywordb: this is the second keywordb
...
The thing is, that after the keyword and the ':'
any char should be possible (the keyword, too)
I've tried many thing but I think I'm still not that into the lexer and parser thinking...
My last idea failed:
rule
: 'keyworda' ':' anychar* 'keywordb' ':' anychar* EOF
;
anychar
: .
;
NEWLINE
: ('\r'? '\n') {$channel=HIDDEN;}
;
EDIT
First of all: thanks for your answer!
I read through the manual and looked the tutorials by scott stanchfield.
The problem is, that I don't get the "anychar" thing!
You are right, the grammar I postet above was wrong because I was in hurry.
A better try is this ahead. Problem is still, that the Tokenizer recognizes e.g. keyworda in a definition ala
keyworda : this is keyworda.
keywordb : this is another key!
...
The grammar:
rule
: KEYA ':' STRING_LITERAL* NEWLINE
keybdefinition*
EOF
;
keybdefinition
: KEYB ':' STRING_LITERAL* NEWLINE
;
KEYA: 'keyworda';
KEYB:'keywordb';
STRING_LITERAL: 'a'..'z' | 'A'..'Z' | '0'..'9' | ':' | '.' | '&' | '/' | '\\' | ';';
NEWLINE: '\r'? | '\n';
SPACE: (' ' | '\t') {$channel=HIDDEN;};
EDIT II
Oh my god, it's quiet obvious to do it the way you explained it. Don't know why I didn't get it myself! Great thanks Tim for your explanation!
I've got just one more question left: If I define my tokens for the lexer and my grammar for the parser. Is it the common way to check the semantic in the tree parser or in the parser itself?
For example, let's assume I have the same grammar defined as you posted.
keyworda : ab
keywordb : xy
keyworda : ab1
keywordb : xy1
...
Now I want to check if after every keyworda definition a keywordb is defined. Later I do want to check if the meaning if the value is proper. Lets assume we do have a keyword extends : 'keyword value' and I need to check if 'keyword value' is already defined.
I could do this in 2 ways: First, change your grammar rule for the parser and add java code for the checkings right there. Second the grammar stays as it is and I define a tree parser grammer to check those conditions.
I do not really know which way is the better one and what the advantages or disadvantages are...
Thanks a lot for your help