使用antlr3解析SVG路径数据

时间:2014-09-02 19:28:38

标签: parsing svg antlr

SVG路径数据的EBNF语法指定为:

svg-path:
    wsp* moveto-drawto-command-groups? wsp*
moveto-drawto-command-groups:
    moveto-drawto-command-group
    | moveto-drawto-command-group wsp* moveto-drawto-command-groups
moveto-drawto-command-group:
    moveto wsp* drawto-commands?
drawto-commands:
    drawto-command
    | drawto-command wsp* drawto-commands
drawto-command:
    closepath
    | lineto
    | horizontal-lineto
    | vertical-lineto
    | curveto
    | smooth-curveto
    | quadratic-bezier-curveto
    | smooth-quadratic-bezier-curveto
    | elliptical-arc
moveto:
    ( "M" | "m" ) wsp* moveto-argument-sequence
moveto-argument-sequence:
    coordinate-pair
    | coordinate-pair comma-wsp? lineto-argument-sequence
closepath:
    ("Z" | "z")
lineto:
    ( "L" | "l" ) wsp* lineto-argument-sequence
lineto-argument-sequence:
    coordinate-pair
    | coordinate-pair comma-wsp? lineto-argument-sequence
horizontal-lineto:
    ( "H" | "h" ) wsp* horizontal-lineto-argument-sequence
horizontal-lineto-argument-sequence:
    coordinate
    | coordinate comma-wsp? horizontal-lineto-argument-sequence
vertical-lineto:
    ( "V" | "v" ) wsp* vertical-lineto-argument-sequence
vertical-lineto-argument-sequence:
    coordinate
    | coordinate comma-wsp? vertical-lineto-argument-sequence
curveto:
    ( "C" | "c" ) wsp* curveto-argument-sequence
curveto-argument-sequence:
    curveto-argument
    | curveto-argument comma-wsp? curveto-argument-sequence
curveto-argument:
    coordinate-pair comma-wsp? coordinate-pair comma-wsp? coordinate-pair
smooth-curveto:
    ( "S" | "s" ) wsp* smooth-curveto-argument-sequence
smooth-curveto-argument-sequence:
    smooth-curveto-argument
    | smooth-curveto-argument comma-wsp? smooth-curveto-argument-sequence
smooth-curveto-argument:
    coordinate-pair comma-wsp? coordinate-pair
quadratic-bezier-curveto:
    ( "Q" | "q" ) wsp* quadratic-bezier-curveto-argument-sequence
quadratic-bezier-curveto-argument-sequence:
    quadratic-bezier-curveto-argument
    | quadratic-bezier-curveto-argument comma-wsp? 
        quadratic-bezier-curveto-argument-sequence
quadratic-bezier-curveto-argument:
    coordinate-pair comma-wsp? coordinate-pair
smooth-quadratic-bezier-curveto:
    ( "T" | "t" ) wsp* smooth-quadratic-bezier-curveto-argument-sequence
smooth-quadratic-bezier-curveto-argument-sequence:
    coordinate-pair
    | coordinate-pair comma-wsp? smooth-quadratic-bezier-curveto-argument-sequence
elliptical-arc:
    ( "A" | "a" ) wsp* elliptical-arc-argument-sequence
elliptical-arc-argument-sequence:
    elliptical-arc-argument
    | elliptical-arc-argument comma-wsp? elliptical-arc-argument-sequence
elliptical-arc-argument:
    nonnegative-number comma-wsp? nonnegative-number comma-wsp? 
        number comma-wsp flag comma-wsp? flag comma-wsp? coordinate-pair
coordinate-pair:
    coordinate comma-wsp? coordinate
coordinate:
    number
nonnegative-number:
    integer-constant
    | floating-point-constant
number:
    sign? integer-constant
    | sign? floating-point-constant
flag:
    "0" | "1"
comma-wsp:
    (wsp+ comma? wsp*) | (comma wsp*)
comma:
    ","
integer-constant:
    digit-sequence
floating-point-constant:
    fractional-constant exponent?
    | digit-sequence exponent
fractional-constant:
    digit-sequence? "." digit-sequence
    | digit-sequence "."
exponent:
    ( "e" | "E" ) sign? digit-sequence
sign:
    "+" | "-"
digit-sequence:
    digit
    | digit digit-sequence
digit:
    "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
wsp:
    (#x20 | #x9 | #xD | #xA)

为了能够使用antlr3:

,我稍微修改了一下
grammar Paths;

eval : svg_path;

svg_path:
    wsp* moveto_drawto_command_groups? wsp*;
moveto_drawto_command_groups:
    moveto_drawto_command_group
    | moveto_drawto_command_group wsp* moveto_drawto_command_groups;
moveto_drawto_command_group:
    moveto wsp* drawto_commands?;
drawto_commands:
    drawto_command
    | drawto_command wsp* drawto_commands;
drawto_command:
    closepath
    | lineto
    | horizontal_lineto
    | vertical_lineto
    | curveto
    | smooth_curveto
    | quadratic_bezier_curveto
    | smooth_quadratic_bezier_curveto
    | elliptical_arc;
moveto:
    ( 'M' | 'm' ) wsp* moveto_argument_sequence;
moveto_argument_sequence:
    coordinate_pair
    | coordinate_pair comma_wsp? lineto_argument_sequence;
closepath:
    ('Z' | 'z');
lineto:
    ( 'L' | 'l' ) wsp* lineto_argument_sequence;
lineto_argument_sequence:
    coordinate_pair
    | coordinate_pair comma_wsp? lineto_argument_sequence;
horizontal_lineto:
    ( 'H' | 'h' ) wsp* horizontal_lineto_argument_sequence;
horizontal_lineto_argument_sequence:
    coordinate
    | coordinate comma_wsp? horizontal_lineto_argument_sequence;
vertical_lineto:
    ( 'V' | 'v' ) wsp* vertical_lineto_argument_sequence;
vertical_lineto_argument_sequence:
    coordinate
    | coordinate comma_wsp? vertical_lineto_argument_sequence;
curveto:
    ( 'C' | 'c' ) wsp* curveto_argument_sequence;
curveto_argument_sequence:
    curveto_argument
    | curveto_argument comma_wsp? curveto_argument_sequence;
curveto_argument:
    coordinate_pair comma_wsp? coordinate_pair comma_wsp? coordinate_pair;
smooth_curveto:
    ( 'S' | 's' ) wsp* smooth_curveto_argument_sequence;
smooth_curveto_argument_sequence:
    smooth_curveto_argument
    | smooth_curveto_argument comma_wsp? smooth_curveto_argument_sequence;
smooth_curveto_argument:
    coordinate_pair comma_wsp? coordinate_pair;
quadratic_bezier_curveto:
    ( 'Q' | 'q' ) wsp* quadratic_bezier_curveto_argument_sequence;
quadratic_bezier_curveto_argument_sequence:
    quadratic_bezier_curveto_argument
    | quadratic_bezier_curveto_argument comma_wsp? 
        quadratic_bezier_curveto_argument_sequence;
quadratic_bezier_curveto_argument:
    coordinate_pair comma_wsp? coordinate_pair;
smooth_quadratic_bezier_curveto:
    ( 'T' | 't' ) wsp* smooth_quadratic_bezier_curveto_argument_sequence;
smooth_quadratic_bezier_curveto_argument_sequence:
    coordinate_pair
    | coordinate_pair comma_wsp? smooth_quadratic_bezier_curveto_argument_sequence;
elliptical_arc:
    ( 'A' | 'a' ) wsp* elliptical_arc_argument_sequence;
elliptical_arc_argument_sequence:
    elliptical_arc_argument
    | elliptical_arc_argument comma_wsp? elliptical_arc_argument_sequence;
elliptical_arc_argument:
    nonnegative_number comma_wsp? nonnegative_number comma_wsp? 
        number comma_wsp flag comma_wsp? flag comma_wsp? coordinate_pair;
coordinate_pair:
    coordinate comma_wsp? coordinate;
coordinate:
    number;
nonnegative_number:
    integer_constant
    | floating_point_constant;
number:
    sign? integer_constant
    | sign? floating_point_constant;
flag:
    '0' | '1';
comma_wsp:
    (wsp+ comma? wsp*) | (comma wsp*);
comma:
    ',';
integer_constant:
    digit_sequence;
floating_point_constant:
    fractional_constant exponent?
    | digit_sequence exponent;
fractional_constant:
    digit_sequence? '.' digit_sequence
    | digit_sequence '.';
exponent:
    ( 'e' | 'E' ) sign? digit_sequence;
sign:
    '+' | '-';
digit_sequence:
    digit
    | digit digit_sequence;
digit:
    ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
wsp:
    (' ' | '\t' | '\r'| '\n');

但是当我尝试生成解析器时,我收到以下警告和错误:

warning(200): Paths.g:6:5: Decision can match input such as "' '..'\n'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
error(211): Paths.g:7:29: [fatal] rule moveto_drawto_command_groups has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
warning(200): Paths.g:11:12: Decision can match input such as "' '..'\n'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
error(211): Paths.g:12:16: [fatal] rule drawto_commands has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:27:25: [fatal] rule moveto_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:34:25: [fatal] rule lineto_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:39:36: [fatal] rule horizontal_lineto_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
warning(200): Paths.g:39:36: Decision can match input such as "'+'..'-' {'0'..'1', '2'..'9'}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
error(211): Paths.g:44:34: [fatal] rule vertical_lineto_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
warning(200): Paths.g:44:34: Decision can match input such as "'+'..'-' {'0'..'1', '2'..'9'}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
error(211): Paths.g:49:26: [fatal] rule curveto_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:56:33: [fatal] rule smooth_curveto_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:63:43: [fatal] rule quadratic_bezier_curveto_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:71:50: [fatal] rule smooth_quadratic_bezier_curveto_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:76:33: [fatal] rule elliptical_arc_argument_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
warning(200): Paths.g:86:19: Decision can match input such as "{'0'..'1', '2'..'9'}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): Paths.g:89:7: Decision can match input such as "'+'..'-' {'0'..'1', '2'..'9'}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): Paths.g:95:6: Decision can match input such as "' '..'\n'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
error(211): Paths.g:100:24: [fatal] rule floating_point_constant has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:103:20: [fatal] rule fractional_constant has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.
error(211): Paths.g:110:15: [fatal] rule digit_sequence has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.

我使用antlr的方式是错误的,我对原始语法的修改是不正确的,还是原始语法不正确?

0 个答案:

没有答案