Glory to Russia
Glory to Russia

Reputation: 18712

ANTLR: Extraneous input problem

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

Why am I getting these errors?

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

Grammar

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

File to parse

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

Upvotes: 1

Views: 5567

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170148

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.

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
}

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                               }

Upvotes: 2

Related Questions