[antlr-interest] Smalltalk grammar

Fede federico.omoto at gmail.com
Sun Dec 30 23:07:15 PST 2007


Hi!

I'm new to ANTLR and I'm having a problem translating a few rules
found here:
http://chronos-st.blogspot.com/2007/12/smalltalk-in-one-page.html

The specific problem is on set subtraction, coudn't find a way to do
it in ANTLR. Sorry if this kind of question was answered before but I
found nothing searching in the ml archives.

I'm sending you the grammar I've written so far (again, this is my
first time with ANTLR, so it's possible there could be lots of
errors).

The problematic rules are:
-BindableIdentifier
-SymbolInArrayLiteral
-LiteralArrayElement

Please, if someone can take a look at it and give me some tips it will be
VERY appreciated :)


Thank you in advance,

Fede


/////////////////// Smalltalk grammar ///////////////////

grammar Smalltalk;

Character
       :       '\u0000'..'\uFFFE'
       ;

WhitespaceCharacter
       :       (' '|'\t'|'\n'|'\r')
       ;

DecimalDigit
       :       '0'..'9'
       ;

Letter
       :       ('a'..'z' | 'A'..'Z')
       ;

CommentCharacter
       :       ~'"'
       ;

Comment
       :       '"' CommentCharacter* '"'
       ;

OptionalWhitespace
       :       (WhitespaceCharacter | Comment)*
       ;

Whitespace
       :       (WhitespaceCharacter | Comment) OptionalWhitespace
       ;

LetterOrDigit
       :       DecimalDigit | Letter
       ;

Identifier
       :       (Letter | '_') (LetterOrDigit | '_')*
       ;

Reference
       :       Identifier
       ;

ConstantReference
       :       'nil' | 'false' | 'true'
       ;

PseudoVariableReference
       :       'self' | 'super' | 'thisContext'
       ;

ReservedIdentifier
       :       PseudoVariableReference | ConstantReference
       ;

// FIXME
BindableIdentifier
       :       Identifier - ReservedIdentifier
       ;

UnaryMessageSelector
       :       Identifier
       ;

Keyword :       Identifier ':'
       ;

KeywordMessageSelector
       :        Keyword Keyword*
       ;

BinarySelectorChar
       :       '~' | '!' | '@' | '%' | '&' | '*' | '-' | '+' | '=' | '|' |
'\\' |
'<' | '>' | ',' | '?' | '/'
       ;

BinaryMessageSelector
       :       BinarySelectorChar BinarySelectorChar?
       ;

IntegerLiteral
       :       '-'? UnsignedIntegerLiteral
       ;

UnsignedIntegerLiteral
       :       DecimalIntegerLiteral | Radix 'r' BaseNIntegerLiteral
       ;

DecimalIntegerLiteral
       :       DecimalDigit DecimalDigit*
       ;

Radix
       :       DecimalIntegerLiteral
       ;

BaseNIntegerLiteral
       :       LetterOrDigit LetterOrDigit*
       ;

ScaledDecimalLiteral
       :       '-'? DecimalIntegerLiteral ('.' DecimalIntegerLiteral)? 's'
DecimalIntegerLiteral?
       ;

FloatingPointLiteral
       :       '-'? DecimalIntegerLiteral ('.' DecimalIntegerLiteral
Exponent? |
Exponent)
       ;

Exponent
       :       ('e' | 'd' | 'q') (('-')? DecimalIntegerLiteral)?
       ;

CharacterLiteral
       :       '$' Character
       ;

StringLiteral
       :       '\'' (StringLiteralCharacter | '\'\'')* '\''
       ;

StringLiteralCharacter
       :       ~'\''
       ;

// FIXME
SymbolInArrayLiteral
       :       UnaryMessageSelector - ConstantReference |
KeywordMessageSelector |
BinaryMessageSelector
       ;

SymbolLiteral
       :       '#' (SymbolInArrayLiteral | ConstantReference |
StringLiteral)
       ;

ArrayLiteral
       :       ObjectArrayLiteral | ByteArrayLiteral
       ;

ObjectArrayLiteral
       :       '#' NestedObjectArrayLiteral
       ;

NestedObjectArrayLiteral
       :       '(' OptionalWhitespace (LiteralArrayElement (Whitespace
LiteralArrayElement)*)? OptionalWhitespace ')'
       ;

// FIXME
LiteralArrayElement
       :       literal - blockLiteral | NestedObjectArrayLiteral |
SymbolInArrayLiteral | ConstantReference
       ;

ByteArrayLiteral
       :       '#[' OptionalWhitespace (UnsignedIntegerLiteral (Whitespace
UnsignedIntegerLiteral)*)? OptionalWhitespace ']'
       ;

formalBlockArgumentDeclaration
       :       ':' BindableIdentifier
       ;

formalBlockArgumentDeclarationList
       :       formalBlockArgumentDeclaration (Whitespace
formalBlockArgumentDeclaration)*
       ;

blockLiteral
       :       '[' (OptionalWhitespace formalBlockArgumentDeclarationList
OptionalWhitespace '|')? executableCode OptionalWhitespace ']'
       ;

literal
       :       ConstantReference | IntegerLiteral | ScaledDecimalLiteral |
FloatingPointLiteral | CharacterLiteral | StringLiteral |
SymbolLiteral | ArrayLiteral | blockLiteral
       ;

nestedExpression
       :       '(' statement OptionalWhitespace ')'
       ;

operand
       :       literal | Reference | nestedExpression
       ;

unaryMessage
       :       UnaryMessageSelector
       ;

unaryMessageChain
       :       (OptionalWhitespace unaryMessage)*
       ;

binaryMessageOperand
       :       operand unaryMessageChain
       ;

binaryMessage
       :       BinaryMessageSelector OptionalWhitespace binaryMessageOperand
       ;

binaryMessageChain
       :       (OptionalWhitespace binaryMessage)*
       ;

keywordMessageArgument
       :       binaryMessageOperand binaryMessageChain
       ;

keywordMessageSegment
       :       Keyword OptionalWhitespace keywordMessageArgument
       ;

keywordMessage
       :       keywordMessageSegment (OptionalWhitespace
keywordMessageSegment)*
       ;

messageChain
       :       unaryMessage unaryMessageChain binaryMessageChain
keywordMessage? |
binaryMessage binaryMessageChain keywordMessage? | keywordMessage
       ;

cascadedMessage
       :       ';' OptionalWhitespace messageChain
       ;

expression
       :       operand (OptionalWhitespace messageChain (OptionalWhitespace
cascadedMessage)*)?
       ;

assignmentOperation
       :       OptionalWhitespace BindableIdentifier OptionalWhitespace ':='
       ;

statement
       :       assignmentOperation* OptionalWhitespace expression
       ;

methodReturnOperator
       :       OptionalWhitespace '^'
       ;

finalStatement
       :       methodReturnOperator? statement
       ;

localVariableDeclarationList
       :       OptionalWhitespace '|' OptionalWhitespace (BindableIdentifier
(Whitespace BindableIdentifier)*)? OptionalWhitespace '|'
       ;

executableCode
       :       localVariableDeclarationList? ((statement OptionalWhitespace
'.')*
finalStatement '.'?)?
       ;

unaryMethodHeader
       :       UnaryMessageSelector
       ;

binaryMethodHeader
       :       BinaryMessageSelector OptionalWhitespace BindableIdentifier
       ;

keywordMethodHeaderSegment
       :       Keyword OptionalWhitespace BindableIdentifier
       ;

keywordMethodHeader
       :       keywordMethodHeaderSegment (Whitespace
keywordMethodHeaderSegment)*
       ;

methodHeader
       :       unaryMethodHeader | binaryMethodHeader | keywordMethodHeader
       ;

methodDeclaration
       :       OptionalWhitespace methodHeader executableCode
       ;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071231/57ae1418/attachment.html 


More information about the antlr-interest mailing list