ANTLR:无关的输入问题

时间:2011-09-04 19:52:45

标签: antlr

我有一个语法文件Bookings.g和我尝试用该语法解析的文件。

为什么我会收到这些错误?

第1:12行无关输入'“pcc”'期待字符串 第3行:0输入'}'不匹配期望ScenarioPart1 第7:13行不匹配输入'“R”'期待字符串 9:8不匹配的输入'“Doll”'期待String 第15:8行不匹配的输入'“Train”'期待字符串 第21:8行不匹配输入'“Ball”'期待字符串 第34:0行'}'缺少EOF

语法

grammar Bookings;

@members
{
    private DefaultBookingsFile bookingsFile;

    public DefaultBookingsFile getBookingsFile()
    {
        return this.bookingsFile;
    }

}

@header { 

package at.silverstrike.pcc.impl.tj3bookingsparser.grammar; 

import org.slf4j.Logger;

}

@lexer::header {
package at.silverstrike.pcc.impl.tj3bookingsparser.grammar;
}

bookingsFile
  : 
    {
        this.bookingsFile = new DefaultBookingsFile();
    }
  header
  projectIds
  resourceDeclaration
  task+
  (
    suppTask=supplementTask { this.bookingsFile.addSupplementStatement( $suppTask.suppStmt ); }
  )*
  supplementResource*
  EOF
  ;

header
    :
    Project Prj String String DateTimeWithTimeZone Hyphen DateTimeWithTimeZone OpenParen TimeZone Utc 
    ScenarioPart1 OpenParen ScenarioPart2 CloseParen 
    CloseParen
    ;

ScenarioPart1
    :
    'scenario plan "Plan"'
    ;

ScenarioPart2
    :
    'active yes'
    ;

TimeZone
    :
    'timezone'
    ;

Utc
    :
    '"'A+'"'
    ;

projectIds:
    Projectids Prj
    ;

resourceDeclaration
    : Resource Identifier String
    ;

task
    :
    Task Identifier String OpenParen
    (task)* 
    (Start DateTimeWithTimeZone
    End DateTimeWithTimeZone
    Scheduling Asap
    Scheduled)*
    CloseParen
    ;

/* (Complete FloatingPointNumber)* */

supplementTask returns [DefaultSupplementStatement suppStmt]
    :
        {
            suppStmt = new DefaultSupplementStatement();
        }   
    Supplement Task taskId=Identifier {suppStmt.setTaskId($taskId.text); } 
    OpenParen
    (
    bStmt=booking {suppStmt.addBookingStatement( $bStmt.stmt ); }
    )*  
    Priority IntegerNumber
    CloseParen
    ;

supplementResource
    :
    Supplement Resource Identifier OpenParen
    workinghours+
    CloseParen
    ;

workinghours
    :
    Workinghours DayOfWeek (Off|workingIntervals)
    ;

Workinghours
    : 'workinghours'
    ;

workingIntervals
    :
    workingInterval (Comma workingInterval)*
    ; 

workingInterval
    : Time Hyphen Time
    ;

Time
    : IntegerNumber Colon IntegerNumber
    ; 

Colon
    :
    ':'
    ;

Comma
    :
    ','
    ;

DayOfWeek
    : 'mon'
    | 'tue'
    | 'wed'
    | 'thu'
    | 'fri'
    | 'sat'
    | 'sun'
    ;

Off
    :
    'off'
    ;

booking  returns [DefaultBookingStatement stmt]
    :
    {
        stmt = new DefaultBookingStatement();
    }
    Booking resource=Identifier { stmt.setResource($resource.text); }  
    bt1=bookingTime { stmt.addIndBooking($bt1.indBooking); } 
    (Comma 
    bt2=bookingTime  { stmt.addIndBooking($bt2.indBooking); } 
    )*
    (OpenParen 
    overtime 
    CloseParen)
    ;

bookingTime returns [DefaultIndBooking indBooking]
    :
    startTime=DateTimeWithTimeZone 
    Plus 
    bookingDuration=FloatingPointNumberDuration
    {
        $indBooking = new DefaultIndBooking($startTime.text, $bookingDuration.text);
    }
    ;

Booking
    :
    'booking'
    ;

Plus
    :
    '+'
    ;

duration
    :
    FloatingPointNumber 'h'
    ;

overtime
    :
    Overtime IntegerNumber
    ;

Overtime
    :
    'overtime'
    ;

Supplement
    :
    'supplement'
    ;

Priority
    :
    'priority'
    ;

Complete
    :
    'complete'
    ;

Start
    :
    'start'
    ;

End
    :
    'end'
    ;

Scheduling
    :
    'scheduling'
    ;

Asap
    :
    'asap'
    ;

Scheduled
    :
    'scheduled'
    ;

Task
    :
    'task'
    ;

Resource
    :
    'resource'
    ;

Projectids
    :
    'projectids'
    ;

Project
  :  'project'
  ;

Prj
  :  'prj'
  ;

OpenParen
  :  '{'
  ;

CloseParen
  :  '}'
  ;

Hyphen
  :  '-'
  ;

FloatingPointNumber
  : D+'.'D+
  ;
FloatingPointNumberDuration
  : D+ P D+ H
  ;

IntegerNumber
  : D+
  ;

Identifier
  : (D|A|P)+;

String
  :  '"' ~'"'* '"'
  ;

DateTimeWithTimeZone
  :  D D D D '-' D D '-' D D '-' D D ':' D D '-+' D D D D
  ;  

fragment    
D
  :  '0'..'9'
  ;

fragment
A
  : 'A'..'Z'
  | 'a'..'z'
  ;

fragment
P
  : '.'
  ;

fragment
H
  : 'h'
  ;


Space   
  :  (' ' | '\t' | '\r'? '\n'){$channel=HIDDEN;}
  ;

要解析的文件

project prj "pcc" "1.0" 2011-09-04-00:00-+0000 - 2011-10-04-10:00-+0000 {
  timezone "UTC"
  scenario plan "Plan" {
    active yes
  }
}

projectids prj

resource R62 "R"

task T1 "Doll" {
  start 2011-09-05-14:00-+0000
  end 2011-09-05-16:00-+0000
  scheduling asap
  scheduled
}
task T2 "Train" {
  start 2011-09-05-11:00-+0000
  end 2011-09-05-14:00-+0000
  scheduling asap
  scheduled
}
task T4 "Ball" {
  task T3 "Big ball" {
    start 2011-09-05-09:00-+0000
    end 2011-09-05-11:00-+0000
    scheduling asap
    scheduled
  }
  task T5 "Small ball" {
    start 2011-09-05-16:00-+0000
    end 2011-09-05-17:00-+0000
    scheduling asap
    scheduled
  }
}
supplement task T1 {
  booking R62 2011-09-05-14:00-+0000 + 2.0h { overtime 2 }
  priority 997
  projectid prj
}
supplement task T2 {
  booking R62 2011-09-05-11:00-+0000 + 3.0h { overtime 2 }
  priority 998
  projectid prj
}
supplement task T4 {
  priority 0
  projectid prj
}
supplement task T4.T3 {
  booking R62 2011-09-05-09:00-+0000 + 2.0h { overtime 2 }
  priority 1000
  projectid prj
}
supplement task T4.T5 {
  booking R62 2011-09-05-16:00-+0000 + 1.0h { overtime 2 }
  priority 996
  projectid prj
}
supplement resource R62 {
  workinghours sun off
               workinghours mon 9:00 - 17:00
               workinghours tue 9:00 - 17:00
               workinghours wed 9:00 - 17:00
               workinghours thu 9:00 - 17:00
               workinghours fri 9:00 - 17:00
               workinghours sat off
}

UPD,05.09.2011 00:24 CEST:这是有效的更正语法文件。

grammar Bookings;

@members
{
    private DefaultBookingsFile bookingsFile;

    public DefaultBookingsFile getBookingsFile()
    {
        return this.bookingsFile;
    }

}

@header { 

package at.silverstrike.pcc.impl.tj3bookingsparser.grammar; 

import org.slf4j.Logger;

}

@lexer::header {
package at.silverstrike.pcc.impl.tj3bookingsparser.grammar;
}

bookingsFile
  : 
    {
        this.bookingsFile = new DefaultBookingsFile();
    }
  header
  projectIds
  resourceDeclaration
  task+
  (
    suppTask=supplementTask { this.bookingsFile.addSupplementStatement( $suppTask.suppStmt ); }
  )*
  supplementResource*
  EOF
  ;

header
    :
    Project Prj String String DateTimeWithTimeZone Hyphen DateTimeWithTimeZone OpenParen TimeZone String 
    ScenarioPart1 OpenParen ScenarioPart2 CloseParen 
    CloseParen
    ;

ScenarioPart1
    :
    'scenario plan "Plan"'
    ;

ScenarioPart2
    :
    'active yes'
    ;

TimeZone
    :
    'timezone'
    ;

projectIds:
    Projectids Prj
    ;

resourceDeclaration
    : Resource Identifier String
    ;

task
    :
    Task Identifier String OpenParen
    (task)* 
    (Start DateTimeWithTimeZone
    End DateTimeWithTimeZone
    Scheduling Asap
    Scheduled)*
    CloseParen
    ;

/* (Complete FloatingPointNumber)* */

supplementTask returns [DefaultSupplementStatement suppStmt]
    :
        {
            suppStmt = new DefaultSupplementStatement();
        }   
    Supplement Task taskId=Identifier {suppStmt.setTaskId($taskId.text); } 
    OpenParen
    (
    bStmt=booking {suppStmt.addBookingStatement( $bStmt.stmt ); }
    )*  
    Priority IntegerNumber
    ProjectIdPrj
    CloseParen
    ;

supplementResource
    :
    Supplement Resource Identifier OpenParen
    workinghours+
    CloseParen
    ;

workinghours
    :
    Workinghours DayOfWeek (Off|workingIntervals)
    ;

Workinghours
    : 'workinghours'
    ;

workingIntervals
    :
    workingInterval (Comma workingInterval)*
    ; 

workingInterval
    : Time Hyphen Time
    ;

Time
    : IntegerNumber Colon IntegerNumber
    ; 

Colon
    :
    ':'
    ;

Comma
    :
    ','
    ;

DayOfWeek
    : 'mon'
    | 'tue'
    | 'wed'
    | 'thu'
    | 'fri'
    | 'sat'
    | 'sun'
    ;

Off
    :
    'off'
    ;

booking  returns [DefaultBookingStatement stmt]
    :
    {
        stmt = new DefaultBookingStatement();
    }
    Booking resource=Identifier { stmt.setResource($resource.text); }  
    bt1=bookingTime { stmt.addIndBooking($bt1.indBooking); } 
    (Comma 
    bt2=bookingTime  { stmt.addIndBooking($bt2.indBooking); } 
    )*
    (OpenParen 
    overtime 
    CloseParen)
    ;

bookingTime returns [DefaultIndBooking indBooking]
    :
    startTime=DateTimeWithTimeZone 
    Plus 
    bookingDuration=FloatingPointNumberDuration
    {
        $indBooking = new DefaultIndBooking($startTime.text, $bookingDuration.text);
    }
    ;

ProjectIdPrj
    :
    'projectid prj'
    ;

Booking
    :
    'booking'
    ;

Plus
    :
    '+'
    ;

duration
    :
    FloatingPointNumber 'h'
    ;

overtime
    :
    Overtime IntegerNumber
    ;

Overtime
    :
    'overtime'
    ;

Supplement
    :
    'supplement'
    ;

Priority
    :
    'priority'
    ;

Complete
    :
    'complete'
    ;

Start
    :
    'start'
    ;

End
    :
    'end'
    ;

Scheduling
    :
    'scheduling'
    ;

Asap
    :
    'asap'
    ;

Scheduled
    :
    'scheduled'
    ;

Task
    :
    'task'
    ;

Resource
    :
    'resource'
    ;

Projectids
    :
    'projectids'
    ;

Project
  :  'project'
  ;

Prj
  :  'prj'
  ;

OpenParen
  :  '{'
  ;

CloseParen
  :  '}'
  ;

Hyphen
  :  '-'
  ;

FloatingPointNumber
  : D+'.'D+
  ;
FloatingPointNumberDuration
  : D+ P D+ H
  ;

IntegerNumber
  : D+
  ;

Identifier
  : (D|A|P)+;

String
  :  '"' ~'"'* '"'
  ;

DateTimeWithTimeZone
  :  D D D D '-' D D '-' D D '-' D D ':' D D '-+' D D D D
  ;  

fragment    
D
  :  '0'..'9'
  ;

fragment
A
  : 'A'..'Z'
  | 'a'..'z'
  ;

fragment
P
  : '.'
  ;

fragment
H
  : 'h'
  ;


Space   
  :  (' ' | '\t' | '\r'? '\n'){$channel=HIDDEN;}
  ;

1 个答案:

答案 0 :(得分:2)

您的所有令牌'"pcc"''"R"''"Doll"''"Train"''"Ball"' 标记为{{1但是作为String令牌,会导致它们失败您的生产(解析器)规则,这些规则会导致Utc令牌。

此外,您的String规则有误。它看起来像:

supplementTask

但您的输入如下:

supplementTask
  : Supplement Task Identifier
    OpenParen
    booking*  
    Priority IntegerNumber
    CloseParen
  ;

例如,在您的supplement task T1 { booking R62 2011-09-05-14:00-+0000 + 2.0h { overtime 2 } priority 997 projectid prj } Priority令牌之后,没有IntegerNumber,但是源CloseParen

仅供参考,这是你的词作者产生的代币清单:

Project                                  project
Prj                                      prj
Utc                                      "pcc"
String                                   "1.0"
DateTimeWithTimeZone                     2011-09-04-00:00-+0000
Hyphen                                   -
DateTimeWithTimeZone                     2011-10-04-10:00-+0000
OpenParen                                {
TimeZone                                 timezone
Utc                                      "UTC"
ScenarioPart1                            scenario plan "Plan"
OpenParen                                {
ScenarioPart2                            active yes
CloseParen                               }
CloseParen                               }
Projectids                               projectids
Prj                                      prj
Resource                                 resource
Identifier                               R62
Utc                                      "R"
Task                                     task
Identifier                               T1
Utc                                      "Doll"
OpenParen                                {
Start                                    start
DateTimeWithTimeZone                     2011-09-05-14:00-+0000
End                                      end
DateTimeWithTimeZone                     2011-09-05-16:00-+0000
Scheduling                               scheduling
Asap                                     asap
Scheduled                                scheduled
CloseParen                               }
Task                                     task
Identifier                               T2
Utc                                      "Train"
OpenParen                                {
Start                                    start
DateTimeWithTimeZone                     2011-09-05-11:00-+0000
End                                      end
DateTimeWithTimeZone                     2011-09-05-14:00-+0000
Scheduling                               scheduling
Asap                                     asap
Scheduled                                scheduled
CloseParen                               }
Task                                     task
Identifier                               T4
Utc                                      "Ball"
OpenParen                                {
Task                                     task
Identifier                               T3
String                                   "Big ball"
OpenParen                                {
Start                                    start
DateTimeWithTimeZone                     2011-09-05-09:00-+0000
End                                      end
DateTimeWithTimeZone                     2011-09-05-11:00-+0000
Scheduling                               scheduling
Asap                                     asap
Scheduled                                scheduled
CloseParen                               }
Task                                     task
Identifier                               T5
String                                   "Small ball"
OpenParen                                {
Start                                    start
DateTimeWithTimeZone                     2011-09-05-16:00-+0000
End                                      end
DateTimeWithTimeZone                     2011-09-05-17:00-+0000
Scheduling                               scheduling
Asap                                     asap
Scheduled                                scheduled
CloseParen                               }
CloseParen                               }
Supplement                               supplement
Task                                     task
Identifier                               T1
OpenParen                                {
Booking                                  booking
Identifier                               R62
DateTimeWithTimeZone                     2011-09-05-14:00-+0000
Plus                                     +
FloatingPointNumberDuration              2.0h
OpenParen                                {
Overtime                                 overtime
IntegerNumber                            2
CloseParen                               }
Priority                                 priority
IntegerNumber                            997
Identifier                               projectid
Prj                                      prj
CloseParen                               }
Supplement                               supplement
Task                                     task
Identifier                               T2
OpenParen                                {
Booking                                  booking
Identifier                               R62
DateTimeWithTimeZone                     2011-09-05-11:00-+0000
Plus                                     +
FloatingPointNumberDuration              3.0h
OpenParen                                {
Overtime                                 overtime
IntegerNumber                            2
CloseParen                               }
Priority                                 priority
IntegerNumber                            998
Identifier                               projectid
Prj                                      prj
CloseParen                               }
Supplement                               supplement
Task                                     task
Identifier                               T4
OpenParen                                {
Priority                                 priority
IntegerNumber                            0
Identifier                               projectid
Prj                                      prj
CloseParen                               }
Supplement                               supplement
Task                                     task
Identifier                               T4.T3
OpenParen                                {
Booking                                  booking
Identifier                               R62
DateTimeWithTimeZone                     2011-09-05-09:00-+0000
Plus                                     +
FloatingPointNumberDuration              2.0h
OpenParen                                {
Overtime                                 overtime
IntegerNumber                            2
CloseParen                               }
Priority                                 priority
IntegerNumber                            1000
Identifier                               projectid
Prj                                      prj
CloseParen                               }
Supplement                               supplement
Task                                     task
Identifier                               T4.T5
OpenParen                                {
Booking                                  booking
Identifier                               R62
DateTimeWithTimeZone                     2011-09-05-16:00-+0000
Plus                                     +
FloatingPointNumberDuration              1.0h
OpenParen                                {
Overtime                                 overtime
IntegerNumber                            2
CloseParen                               }
Priority                                 priority
IntegerNumber                            996
Identifier                               projectid
Prj                                      prj
CloseParen                               }
Supplement                               supplement
Resource                                 resource
Identifier                               R62
OpenParen                                {
Workinghours                             workinghours
DayOfWeek                                sun
Off                                      off
Workinghours                             workinghours
DayOfWeek                                mon
Time                                     9:00
Hyphen                                   -
Time                                     17:00
Workinghours                             workinghours
DayOfWeek                                tue
Time                                     9:00
Hyphen                                   -
Time                                     17:00
Workinghours                             workinghours
DayOfWeek                                wed
Time                                     9:00
Hyphen                                   -
Time                                     17:00
Workinghours                             workinghours
DayOfWeek                                thu
Time                                     9:00
Hyphen                                   -
Time                                     17:00
Workinghours                             workinghours
DayOfWeek                                fri
Time                                     9:00
Hyphen                                   -
Time                                     17:00
Workinghours                             workinghours
DayOfWeek                                sat
Off                                      off
CloseParen                               }