Question

this is probably a really obvious question but I can't seem to find any help on it. At the moment I have a grammar to type in simple commands to be broken down into HTML later via Java

grammar CoCodemol;
r : file;
file
  :  line*
  ;

line
  : Newtype
  | Assignment
  | Clear (LineBreak)?
  | End (LineBreak)?
  ;

Newtype
   : Types Space Num* Space? '=' Space? 'new' Space Types (LineBreak)?
   ;

Assignment
   : Num* '.' Variables Space? '=' Space? NumS* (LineBreak)?
   ;

Types
  : 'body'
  | 'div'
  | 'span'
  | 'html'
  ;

Variables
  : 'width'
  | 'height'
  | 'background'
  | 'margin'
  | 'margin-left'
  | 'margin-right'
  | 'margin-top'
  | 'margin-bottom'
  | 'padding'
  | 'padding-left'
  | 'padding-right'
  | 'padding-top'
  | 'padding-bottom'
  | 'float'
  ;

Declarations
  : 'new'
  ;

End
  : 'end' (LineBreak)?
  ;

Clear
  : 'clear' (LineBreak)?
  ;

Num : ([0-9]|'a'..'z'|'A'..'Z')+;
NumS : ([0-9]|'a'..'z'|'A'..'Z'|' ')+;



Space
  : ' '
  ;


LineBreak
  : '\r'? '\n'
  | '\r'
  ;

This is used in conjunction with Java to read a file containing:

body Body = new body
Body.margin = 0
Body.padding = 0
div Container = new div
Container.width = 900px
Container.margin = 0 auto
div Header = new div
Header.width = 890px
Header.padding = 0px
end
div Leftbox = new div
Leftbox.width = 200px
Leftbox.float = left
end
div Rightbox = new div
Rightbox.width = 700px
Rightbox.float = right
end
clear
end
end

But when I loop through the lexer to print all tokens, I only receive Assignment, Newtype, Clear and End, which simply contain the lines. I wanted the tokens to be a bit more in-depth and tell Java which type each of the words matched up to (Num, Variable etc) but the lines (Assignment, Newtype, Clear and End) seem to be the only ones showing up.

Any suggestions? (I know the grammar is tacky, first attempt while I try to learn the language)

Was it helpful?

Solution

You have asked ANTLR to match almost everything as tokens, such as Newtype. Use newType as a parser rule not lexer rule.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top