Well-Parenthesized-Strings/assignment.jj

43 lines
986 B
Plaintext

PARSER_BEGIN(Assignment)
class Assignment {
public static void main(String[] args) throws ParseException, TokenMgrError
{
try {
Assignment parser = new Assignment(System.in);
parser.Start();
System.out.println("PASS");
} catch (ParseException e) {
System.out.println("FAIL");
System.err.println(e.currentToken.beginLine);
}
}
}
PARSER_END(Assignment)
TOKEN : { <LEFT_PAREN: "(">}
TOKEN : { <RIGHT_PAREN: ")">}
TOKEN : { <NEWLINE: "\n">}
TOKEN : { <ANY: ~[]>}
void Start() : {}
{
(ValidLine()<NEWLINE>)+<EOF>
}
// Single well-parenthesized string
void ValidLine() : {}
{
<LEFT_PAREN>Parens()<RIGHT_PAREN>Parens()
}
// Recursion to allow arbitrary length of parentheses
void Parens() : {}
{
<LEFT_PAREN>Parens()<RIGHT_PAREN>Parens() | Epsilon()
}
void Epsilon() : {}
{
{} // Have to have some kind of body apparently
}