本文介绍了ANTLR:多余的输入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a grammar file Bookings.g and the file I try to parse with that grammar.

为什么会出现这些错误?

Why am I getting these errors?

第1:12行多余的输入"pcc"期望字符串第3:0行的输入'}'不匹配,期望出现ScenarioPart17:13行不匹配的输入'"R"'预期为字符串9:8行不匹配的输入'"Doll"'预期为字符串15:8行不匹配的输入"Train"期望字符串第21:8行不匹配的输入"Ball"期望字符串第34:0行在'}'缺少EOF

line 1:12 extraneous input '"pcc"' expecting Stringline 3:0 mismatched input '}' expecting ScenarioPart1line 7:13 mismatched input '"R"' expecting Stringline 9:8 mismatched input '"Doll"' expecting Stringline 15:8 mismatched input '"Train"' expecting Stringline 21:8 mismatched input '"Ball"' expecting Stringline 34:0 missing EOF at '}'

语法

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,2011年5月9日00:24 CEST:这是正确的语法文件.

UPD, 05.09.2011 00:24 CEST: Here's the corrected grammar file that works.

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;}
  ;

推荐答案

所有令牌'"pcc"''"R"''"Doll"''"Train"''"Ball"'均未标记为 作为String的令牌,但作为Utc令牌的令牌,导致它们无法通过您期望使用String令牌的生产(解析器)规则.

All of your tokens '"pcc"', '"R"', '"Doll"', '"Train"' and '"Ball"' are not tokenized as String's, but as Utc tokens, causing them to fail your production (parser) rules that expect String tokens.

此外,您的supplementTask规则是错误的.看起来像:

Also, your supplementTask rule is wrong. It looks like:

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

但是您的输入看起来像:

yet your input looks like:

supplement task T1 {
  booking R62 2011-09-05-14:00-+0000 + 2.0h { overtime 2 }
  priority 997
  projectid prj
}

例如,在您的PriorityIntegerNumber令牌之后,没有CloseParen,但是有源"projectid prj".

E.g., after your Priority and IntegerNumber tokens, there is no CloseParen, but there's the source "projectid prj".

仅供参考,这是您的词法分析器生成的令牌的列表:

FYI, here's the list of tokens your lexer produces:

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                               }

这篇关于ANTLR:多余的输入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 15:34