Antlr - multiple lexer imports - antlr3

I am trying to build a combined Antlr grammar that has multiple lexer imports. But, this is not working.
I have two lexer files that is being imported in the combined grammar file. But Im getting this error :error(103):parser rule a not allowed in lexer.
lexer grammar LSub ;
SPACE : ' ' ;
lexer grammar L ;
LETTER : 'a'..'z' ;
// SPACE : ' ' ;
NUMBER : '0'..'9' ;
parser grammar P1 ;
letter : LETTER ;
spaces : SPACE+ ;
grammar C ;
import L, LSub, P1 ;
stuff : ( letters spaces )+ ;
LETTER : 'a'..'z' ;
Also, if I try it the two levels of lexer import i.e suppose if I have two lexers L1 and L2, & L2 imports L1 and in the combined grammar if I have imported L2. Then, in the Test Class I get nullpointerexception.
lexer grammar LSub ;
SPACE : ' ' ;
lexer grammar L ;
import LSub;
LETTER : 'a'..'z' ;
// SPACE : ' ' ;
NUMBER : '0'..'9' ;
parser grammar P1 ;
letter : LETTER ;
spaces : SPACE+ ;
parser grammar P2 ;
import P1 ;
letters : letter+ ;
grammar C ;
import L, P2 ;
stuff : ( letters spaces )+ ;
LETTER : 'a'..'z' ;
Is there a solution to this problem?

Multiple lexer are not allowed in any parser file. this is a draw back in antlr 3.5. Try importing LSub in L, then L in P1 . you may get .

Related

How can I do cryptarithmetics in Prolog?

Each of the 7 different letters stands for a different digit. The aim is to find a substitution of digits for the letters such that the resulting sum is arithmetically correct. The solution should then produce all of the combinations of the digits that satisfy the addition problem above. Putting in a query such as crypto(P,I,N,G,O,F,U) should return your solution.
The cryptarithmetic puzzle goes like this:
P I N G
P O N G
+ F U N
---------
I G N I P
Use clpfd! Based on my previous answer to a very similar question, we run the following query:
?- Eq = ([P,I,N,G] + [P,O,N,G] + [F,U,N] #= [I,G,N,I,P]),
crypt_arith_(Eq,Zs),
labeling([],Zs).
Eq = ([7,1,9,4] + [7,0,9,4] + [6,2,9] #= [1,4,9,1,7]), Zs = [7,1,9,4,0,6,2]
; Eq = ([8,1,4,7] + [8,3,4,7] + [9,2,4] #= [1,7,4,1,8]), Zs = [8,1,4,7,3,9,2]
; Eq = ([8,1,4,7] + [8,9,4,7] + [3,2,4] #= [1,7,4,1,8]), Zs = [8,1,4,7,9,3,2]
; false.
Assuming this is a simple substitution cipher we're talking about (and just for fun), I'll take a stab at it. One should note that this is completely untested.
I'm going to set this up in a generic way, so you can say something like:
substitution_cipher( CipherExpr , CipherResult , Expr , Result , Key ).
We'll make the rule that the enciphered stuff is represented by atoms, so you can say something like this:
substitution_cipher( ping + pong + fun , ignip , Expr , Sum , Key ) .
And get the results you'd expect.
So...
First, you need the set (discrete, unique) of characters found in the cipher text:
character_set( Expr , Charset ) :-
setof( C , A^Cs^( atoms_in_expression( Expr , A ) , atom_chars(A,Cs) , member(C,Cs) ) , Charset ) .
atom_in_expression( Expr , Value ) :- atom(Expr) .
atom_in_expression( Expr , Value ) :-
Expr =.. [ _ , Left , Right ] ,
(
values( Left , Value )
;
values( Right, Value
) .
The above walks the parse tree of an expression like a * b + c * d, finding each of the leaf nodes (atoms), deconstructing them into the characters that comprise them. setof/3 ensures that the resulting list is sorted and unique.
Once you have that, you need a way of generating all the possible keys (key == mapping between a character and a digit). We want to be able to say something like
generate_key( [a,b,c] , Key )
and get back
Key = [a:1,b:2,c:3]
etc.
So:
generate_key( Charset , Key ) :-
generate_key( Charset , [] , Key ) .
generate_key( [] , Key , Key ) . % when we've exhausted the character set, we're done.
generate_key( [C|Cs] , Acc , Key ) :- % otherwise...for each character
digit(D) , % find a digit
\+ member( _:D , Acc ) , % that hasn't yet been used
generate_key( Cs , [C:D|Acc] , Key ) % map it to the character and recurse down.
.
digit(D) :- between(0,9,X) , atom_number(D,X).
Then you need a way to decode a cipher expression like
ping + pong + fun
and [try to] turn it back into proper numbers. This isn't much different than walking the parse tree and enumerating the leaf node atoms, but here we need to get them back into numeric form.
If the expression is an atom, we
decompose it into its constituent characters,
using our key, map each character to its corresponding digit,
then we turn that list of digits back into a number
decode( Key , CipherExpr , PlainExpr ) :-
atom(CipherExpression) ,
atom_chars(CipherExpression,Cs) ,
findall( D , ( member(C,Cs), member(C:D,Key) -> true ; D=C ) , Ds ) ,
number_chars( PlainExpr , Ds )
.
The general case is easy. An infix expression like ping + pong is really the prolog term +(ping,pong). We:
Decompose a infix term like ping + pong into an operator (+) and its left and right sub-expressions.
Then we recursively decode the left and right sub-expressions
Finally, we reassemble the [decoded] expression.
decode( Key , CipherExpr , PlainExpr ) :-
CipherExpr =.. [Op,L,R] ,
decode(L,L1) ,
decode(R,R1) ,
PlainExpr =.. [Op,L1,R1]
.
Then you can put it all together:
substitition_cipher( CipherExpr , CipherResult , PlainExpr , PlainResult , Key ) :-
character_set( CipherExpr = CipherResult , Charset ) ,
generate_key( Charset, Key ) ,
decode( Key , CipherExpr , PlainExpr ) ,
decode( Key , CipherResult , PlainResult ) ,
PlainResult =:= PlainExpr
.

Execution order in Prolog: ; operator

I am currently delving into Prolog but coming from a JavaScript background a lot seems strange. For instance, I have a definition such as this:
np # Subject,
pp # IObject ; np # IObject,
However, in executing the result is not as expected. But when using parantheses it is.
np # Subject,
(pp # IObject ; np # IObject),
This seems strange to me, because , is a seperator so parantheses wouldn't be necessary. What is it exactly that parantheses do in Prolog. Please keep in mind that I am very new to Prolog.
, has a lower precedence value than ;. What this means in Prolog, is that an expression like
X ; Y , Z
is interpreted as
X ; (Y , Z)
To group them the other way around, you have to use parenthesis:
(X ; Y) , Z

How can I transform an Extended Backus Naur Grammar to its normal representation?

I have a grammar that contains expressions between brackets '{}' which represents 0 or more times that expression, and expressions between square brackets '[]', which represent 1 or none times that expression, I think this kind of grammars are called Extended Backus-Naur Form Grammars.
I would like to transform the grammar to its normal form (where there are not brackets nor square brackets).
Is there an existing algorithm to do that?
I know that I can substitute something like A--> B[CD]E to A-->BE, A--> BCDE but I would like to know if there are existing algorithms that I could implement in order to transform those expressions.
The most straightforward way to do that is to replace every EBNF construct with a new rule. Here are the equivalences you can use:
Option
A ::= B [C D] E ;
A ::= B X E ;
X ::= C D | ɛ ;
Where ɛ represents the empty string.
Repetition
A ::= B {C D} E ;
Zero or more times:
A ::= B X E ;
X ::= C D X | ɛ ;
One or more times:
A ::= B X E ;
X ::= C D | C D X ;
Grouping
A ::= B (C D) E ;
A ::= B X E ;
X ::= C D ;
Apply these transformations recursively and you'll end up with vanilla BNF.

Unifying a number to a list in Prolog

Alright, so I'm working on a homework assignment, this is supposed to be a four function calculator taking in a list of strings [three, times, two] for example, and output a number. It only considers the numbers from one to twenty in its initial list. The following code is all my own. It runs up to the point where it takes in the last item in the list (that I've been using to test it, but the problem is for any of the inputs) in numberize and then will not unify.
calculator([twenty, times, three, plus, five, divided_by, two],Total).
I know the solution must be an easy one, but I'm not experienced enough yet in Prolog to figure it out.
My question is: how do I fix my code so that it runs the way I want it to?
calculator(X,Total):-
numberize(X,L),
reverse(L,L1),
func(L1,Total).
numberize([X,Y|T],L):-
str2num(X,X1),
numberize(T,[Y,X1|L]).
numberize([X],L):-
str2num(X,X1),
%somehow add on the X1 to the front of L without any errors and it's golden
/*Whatever that line is*/L is [X1|L].
func([X1,X,Z1|T], Total):-
(X == times, times(X1,Z1,Ttl));
(X == plus, plus(X1,Z1,Ttl));
(X == divided_by, divided_by(X1,Z1,Ttl));
(X == minus, minus(X1,Z1,Ttl)),
func([Ttl|T],Total).
str2num(one, X):- X is 1.
str2num(two, X):- X is 2.
str2num(three, X):- X is 3.
str2num(four, X):- X is 4.
str2num(five, X):- X is 5.
str2num(six, X):- X is 6.
str2num(seven, X):- X is 7.
str2num(eight, X):- X is 8.
str2num(nine, X):- X is 9.
str2num(ten, X):- X is 10.
str2num(eleven, X):- X is 11.
str2num(twelve, X):- X is 12.
str2num(thirteen, X):- X is 13.
str2num(fourteen, X):- X is 14.
str2num(fifteen, X):- X is 15.
str2num(sixteen, X):- X is 16.
str2num(seventeen, X):- X is 17.
str2num(eighteen, X):- X is 18.
str2num(nineteen, X):- X is 19.
str2num(twenty, X):- X is 20.
times(X,Y,Prod):-
Prod is X*Y.
plus(X,Y,Sum):-
Sum is X+Y.
divided_by(X,Y,Quo):-
Quo is X/Y.
minus(X,Y,Dif):-
Dif is X-Y.
Small style remark: use facts for str2num/2: just str2num(one, 1). instead of str2num(one, X):- X is 1., etc. Added benefit is that now the predicate can be used both ways, like str2num(Word, 1).
As for the main question, you are almost correct.
The whole numberize predicate can be as simple as this:
numberize([X], [N]) :-
str2num(X, N).
numberize([X, Op | T], [N, Op | NewT]) :-
str2num(X, N),
numberize(T, NewT).
Let's test it:
?- numberize([one, plus, two, minus, three], L).
L = [1, plus, 2, minus, 3]
But you need to remove call to reverse from calculator:
calculator(X,Total):-
numberize(X,L),
func(L,Total).
You have almost correct func predicate. One problem: in Prolog you should have braces around disjunction:
func([X1,X,Z1|T], Total):-
(
X == times, times(X1,Z1,Ttl)
;
X == plus, plus(X1,Z1,Ttl)
;
X == divided_by, divided_by(X1,Z1,Ttl)
;
X == minus, minus(X1,Z1,Ttl)
),
func([Ttl|T],Total).
The second problem: when your list reduced to one number (think how func([1,plus,2], Total) will call func([3], Total) the predicate will fail. All you need to fix this is the rule that Total of a list with just 1 number is the number itself:
func([X], X).
Now the whole thing works:
?- calculator([one, plus, two], Total).
Total = 3
?- calculator([one, plus, two, minus, four], Total).
Total = -1
The way I'd approach this is to start by defining a grammar for arithmetic expressions. The "standard" way of defining grammars is left-recursive. Since prolog does recursive descent parsing, the grammar can't be left-recursive. Every iteration has to remove something from the token stream, lest you go in to the death spiral of infinite recursion. Here's my non-left recursive grammar for a 4-banger calculator like yours:
expression : multiplicative_expression '+' expression
| multiplicative_expression '-' expression
| multiplicative_expression
;
multiplicative_expression : factor '*' multiplicative_expression
| factor '/' multiplicative_expression
| factor '%' multiplicative_expression
| factor
;
factor : '-' value
| '(' expression ')'
| value
;
value : number
Once we have the grammar, the prolog code pretty much writes itself. First, some facts to work with. We need a list of operators and their types (along with the equivalent prolog operator:
operator( plus , additive , '+' ) .
operator( minus , additive , '-' ) .
operator( times , multiplicative , '*' ) .
operator( divided_by , multiplicative , '/' ) .
operator( modulo , multiplicative , 'mod' ) .
And a words-to-numbers-map:
number_word( zero , 0 ).
number_word( one , 1 ).
...
number_word( nineteen , 19 ) .
number_word( twenty , 20 ) .
And we need our interface predicate, calculate/2:
%--------------------------------------------------------------------
% we can calculate a result if Expr is a valid expression
% that consumes all the available tokens in the token stream
%---------------------------------------------------------------------
calculate(Expr,Result) :- expr( Expr , Result , [] ) .
That invokes the "start symbol" of the grammar, expr/3. expr/3 (and the other worker predicates) are pretty much direct restatements of the grammar, with the additional requirement that they need to hand back the unconsumed portion of the input token stream. The parse is successful, if, at the end of the day, the token stream is empty:
expr( Xs , Result , Tail ) :- % per the grammar, an expression is
mult( Xs , LHS , [Sym|X1] ) , % - a multiplicative expression, followed by
operator( Sym , additive , Op ) , % - an infix additive operator, followed by
expr( X1 , RHS , X2 ) , % - another expression
Term =.. [Op,LHS,RHS] , % * in which case, we construct the proper prolog structure
Result is Term , % * in which case, we evaluate the result in the usual way
Tail = X2 % * and unify any remaining tokens with the Tail
. %
expr( Xs , Result , Tail ) :- % alternatively, an expression is simply
mult( Xs , Result , Tail ) % - a single multiplicative expression
. %
The worker predicate for multiplicative terms, mult/3 is pretty much identical — a direct restatement of the grammar:
mult( Xs , Result, Tail ) :- % a multiplicative expression is
factor( Xs , LHS , [Sym|X1] ) , % - a factor, followed by
operator( Sym , multiplicative , Op ) , % - an infix multiplicative operator, followed by
mult( X1 , RHS , X2 ) , % - another factor
evaluate( Op , LHS , RHS , Result ) , % * in which case, we evalute the result in the usual way
Tail = X2 % * and unify any remaining tokens with the tail
. %
mult( Xs , Result , Tail ) :- % alternatively, a multiplicative expression is simply
factor( Xs , Result , Tail ) % - a single factor
. %
Finally, since we're not wrassling with higher-precedence operations like unary minus, exponentiation or parentheses that change operator precedence, a factor is simply a number word that can be converted into an integer value:
factor( [X|Xs] , Value , Xs ) :- % a factor is simply
number_word(X,Value) % - a number value (in our case, a word that we convert to an integer)
.
and a simple helper to evaluate each subexpression as needed:
evaluate( Op , LHS , RHS , Result ) :- % to evaluate an infix term,
Term =.. [Op,LHS,RHS] , % - use univ to convert to the correct prolog structure, and
Result is Term % evaluate it as the result
. %

In Erlang, when do I use ; or , or .?

I have been trying to learn Erlang and have been running into some problems with ending lines in functions and case statements.
When do I use a semicolon (;), comma (,), or period inside my functions or case statements?
I like to read semicolon as OR, comma as AND, full stop as END. So
foo(X) when X > 0; X < 7 ->
Y = X * 2,
case Y of
12 -> bar;
_ -> ook
end;
foo(0) -> zero.
reads as
foo(X) when X > 0 *OR* X < 7 ->
Y = X * 2 *AND*
case Y of
12 -> bar *OR*
_ -> ok
end *OR*
foo(0) -> zero *END*
This should make it clear why there is no ; after the last clause of a case.
Comma at the end of a line of normal code.
Semicolon at the end of case statement, or if statement, etc.
The last case or if statement doesn't have anything at the end.
A period at the end of a function.
example (sorry for the random variable names, clearly this doesn't do anything, but illustrates a point):
case Something of
ok ->
R = 1, %% comma, end of a line inside a case
T = 2; %% semi colon, end of a case, but not the end of the last
error ->
P = 1, %% comma, end of a line inside a case
M = 2 %% nothing, end of the last case
end. %% period, assuming this is the end of the function, comma if not the end of the function
Period (.)
In modules, the period is used to terminate module attributes and function declarations (a.k.a. 'forms'). You can remember this because forms aren't expressions (no value is returned from them), and therefore the period represents the end of a statement.
Keep in mind that definitions of functions with different arities are considered separate statements, so each would be terminated by a period.
For example, the function definitions for hello/0 and hello/1:
hello() -> hello_world.
hello(Greeting) -> Greeting.
(Note that in the erlang shell the period is used to terminate and evaluate expressions, but that is an anomaly.)
Semicolon (;)
The semicolon acts as a clause separator, both for function clauses and expression branches.
Example 1, function clauses:
factorial(0) -> 1;
factorial(N) -> N * fac(N-1).
Example 2, expression branches:
if X < 0 -> negative;
X > 0 -> positive;
X == 0 -> zero
end
Comma (,)
The comma is an expression separator. If a comma follows an expression, it means there's another expression after it in the clause.
hello(Greeting, Name) ->
FullGreeting = Greeting ++ ", " ++ Name,
FullGreeting.
You can think of it like english punctuation. Commas are used to separate things in a series, semicolons are used to separate two very closely related independent clauses[1] (e.g. the different cases of the case statement, function clauses of the same name and arity that match different patterns), and periods are used to end a sentence (complete thought).
Or to prove you went to college. "Do not use semicolons. They are transvestite hermaphrodites representing absolutely nothing. All they do is show you've been to college." -- Kurt Vonnegut
The comma separates expressions, or arguments, or elements of a list/tuple or binary. It is overworked.

Resources