I'm a beginner with ANTLR and i need some help to write the grammar .g file My problem is to parse an URL which can be like this: GET /PROFILE=myprofile;select+name+from+sysdatabases HTTP/1.1 or GET /PROFILE=myprofile;select+name+from+sysdatabases;sp_help;sp_who HTTP/1.1 or GET /PROFILE=myprofile;sp_help;DSN=myDSN;USER=sa;PWD=mypwd;sp_who HTTP/1.1 or GET /DSN=myDSN;USER=sa;PWD=mypwd;sp_who HTTP/1.1 or GET /sp_who HTTP/1.1 or GET /DataSourcesInfos HTTP/1.1 or GET /ProfilesInfos
More>>
I'm a beginner with ANTLR and i need some help to write the grammar .g file
My problem is to parse an URL which can be like this:
GET /PROFILE=myprofile;select+name+from+sysdatabases HTTP/1.1
or
GET /PROFILE=myprofile;select+name+from+sysdatabases;sp_help;sp_who HTTP/1.1
or
GET /PROFILE=myprofile;sp_help;DSN=myDSN;USER=sa;PWD=mypwd;sp_who HTTP/1.1
or
GET /DSN=myDSN;USER=sa;PWD=mypwd;sp_who HTTP/1.1
or
GET /sp_who HTTP/1.1
or
GET /DataSourcesInfos HTTP/1.1
or
GET /ProfilesInfos HTTP/1.1
or
GET /Help HTTP/1.1
I wrote a grammar to do that but when I execute the program, i have
$ java Main
< infile
line 0: expecting METHOD, found ''
$
I don't know how to solve this problem.
If someone can help me, it would be great
Thanks for your help
Hervé
I join to this message, the sample files and the grammar
$ cat infile
GET /PROFILE=myprofile;select+name+from+sysdatabases;DSN=mydsn;USER=sa;PWD=mypwd;sp_helpdb HTTP/1.1
First, i want to parse the URL to extract the following substrings :
PROFILE=myprofile;select+name+from+sysdatabases
and
DSN=mydsn;USER=sa;PWD=mypwd;sp_helpdb
Second, i want to parse again to have
PROFILE=myprofile,
select+name+from+sysdatabases
and
DSN=mydsn
USER=sa
PWD=mypwd
sp_helpdb
$ cat xmlserver.g
class XMLServerParser extends Parser;
// Grammar rules
request_line
: METHOD SLASH (query)+ VERSION
;
query
: profile_sql
| profile_dsn_user_pwd_sql
| dsn_user_pwd_sql
| SQL
| STATS
| DINFOS
| PINFOS
| HELP
;
profile_sql
: kw_profile SEMI SQL
;
profile_dsn_user_pwd_sql
: kw_profile SEMI kw_dsn SEMI kw_user SEMI kw_pwd SEMI SQL
;
dsn_user_pwd_sql
: kw_dsn SEMI kw_user SEMI kw_pwd SEMI SQL
;
kw_profile
: PROFILE EQ TEXT
;
kw_dsn
: DSN EQ TEXT
;
kw_user
: USER EQ TEXT
;
kw_pwd
: PWD EQ TEXT
;
class XMLServerLexer extends Lexer;
options {
caseSensitive=true;
}
// Lexical rules
protected
WS
: (' ' // whitespace not saved
| ' '
| '
' {newline();}
)+
{$setType(Token.SKIP);} // way to set token type
;
protected
DOT
: '.'
;
protected
SEMI
: ';'
;
protected
EQ
: '='
;
protected
SLASH
: '/'
{$setType(Token.SKIP);}
;
protected
DIGIT
: ('0'..'9')
;
protected
CHAR
: ('a'..'z')
;
protected
ALNUM
: (CHAR | DIGIT)
;
protected
TEXT
: (ALNUM)+
;
protected
SQL
: (ALNUM)+
;
protected
METHOD
: ("GET" | "POST")
;
protected
VERSION
: ("HTTP1.1" | "HTTP1.0")
;
protected
PROFILE
: "profile"
;
protected
DSN
: "dsn"
;
protected
USER
: "user"
;
protected
PWD
: "pwd"
;
protected
STATS
: "statistics"
;
protected
DINFOS
: "datasourcesinfos"
;
protected
PINFOS
: "profilesinfos"
;
protected
HELP
: "help"
;
$ javac *.java
$ java antlr.Tool xmlserver.g
ANTLR Parser Generator Version 2.7.1 1989-2000 jGuru.com
xmlserver.g:94: warning: lexical nondeterminism upon
xmlserver.g:94: k==1:'H'
xmlserver.g:94: between alts 1 and 2 of block
xmlserver.g:6: warning: nondeterminism upon
xmlserver.g:6: k==1:PROFILE
xmlserver.g:6: between alts 1 and 2 of block
$ cat Main.java
import java.io.DataInputStream ;
import antlr.* ;
public class Main
{
public static void main(String[] args) {
try
{
XMLServerLexer lexer = new XMLServerLexer(new DataInputStream(System.in));
XMLServerParser parser = new XMLServerParser(lexer);
parser.request_line();11:24 07/09/2001
} catch(Exception e) {
System.err.println(e.getLocalizedMessage()) ;
}
}
}
<<Less