Skip to content

Commit

Permalink
Merge pull request #155 from gapag/master
Browse files Browse the repository at this point in the history
Add ANTLR4 Java backend
  • Loading branch information
gdetrez committed Dec 8, 2015
2 parents b1b9211 + f2eb603 commit ecf4fed
Show file tree
Hide file tree
Showing 20 changed files with 1,298 additions and 291 deletions.
43 changes: 42 additions & 1 deletion docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,47 @@ Cup can only generate parsers with a single entry point. If multiple entry point
are given using the ``entrypoint`` directive, only the first one will be used.
Otherwise, the first category defined in the grammar file will be used as the
entry point for the grammar.
If you need multiple entrypoints, use ANTLRv4.

ANTLRv4
.......

`ANTLRv4 <http://www.antlr.org/>`_ is a parser generator for Java.

With the ``--antlr`` option BNFC generates an ANTLRv4 parser and lexer.

All categories can be entrypoints with ANTLR: the ``entrypoint`` directive is
thus ignored.

Make sure that your system's Java classpath variable points to an ANTLRv4 jar
(`download here <http://www.antlr.org/download.html>`_)

You can use the ANTLR parser generator as follows::

bnfc --java --antlr -m Calc.cf
make

ANTLRv4 returns by default a `parse tree`, which enables you to make use of the
analysis facilities that ANTLR offers.
You can of course still get the usual AST built with the abstract syntax classes
generated by BNFC.

From the ``Calc/Test.java``, generated as a result of the previous commands::

public Calc.Absyn.Exp parse() throws Exception
{
/* The default parser is the first-defined entry point. */
CalcParser.ExpContext pc = p.exp();
// some code in between
Calc.Absyn.Exp ast = pc.result;

The ``pc`` object is a ``ParserRuleContext`` object returned by ANTLR.

It can be used for further analysis through the ANTLR API.

The usual abstract syntax tree returned by BNFC is in the ``result`` field of
any ``ParserRuleContext`` returned by the available parse functions
(here ``exp()``).

Pygments
========
Expand All @@ -40,7 +81,7 @@ script ``setup.py`` for the second option so you can start using the
highlighter right away without fiddling with pygments code.

Here is an example (assuming you've put the Calc grammar in the current
directony)::
directory)::

virtualenv myenv # If you don't use virtualenv, skip the first two steps
source myenv/bin/activate
Expand Down
11 changes: 11 additions & 0 deletions source/src/BNFC/Backend/Common/Makefile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ mkRule target deps recipes =
mkVar :: String -> String -> Doc
mkVar n v = text n <> "=" <> text v

--- | Variable referencing
--
-- >>> mkRefVar "FOO"
-- ${FOO}
mkRefVar :: String -> Doc
mkRefVar m = case m of
"" -> ""
_ -> text $ refVar m

refVar :: String -> String
refVar m = "${" ++ m ++ "}"

-- | Create the Makefile file using the name specified in the option
-- record.
Expand Down
5 changes: 5 additions & 0 deletions source/src/BNFC/Backend/Common/NamedVariables.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ varName c = map toLower c ++ "_"

--this makes var names a little cleaner.
showNum n = if n == 0 then [] else show n

-- Makes the first letter a lowercase.
firstLowerCase :: String -> String
firstLowerCase "" = ""
firstLowerCase (a:b) = (toLower a):b
Loading

7 comments on commit ecf4fed

@johnjcamilleri
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using the Java backend I now get this error:

Unable to open "CPP/CPP.cup" for input

The file is apparently called CPP/_cup.cup. Seems like this regression was introduced with this commit?

@gapag
Copy link

@gapag gapag commented on ecf4fed Feb 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read only now your comment, just because I was looking at this merge (I got no notification). I will try to fix this.
Is it possible to add your test case to the test suite?

@johnjcamilleri
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this happens with any grammar when using the Java backend.
Maybe the automatic tests don't test the Java backend at all?

@gapag
Copy link

@gapag gapag commented on ecf4fed Feb 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gdetrez The automatic tests should, as I read from their logs.

I cloned anew the repository on a computer that had never known BNFC in any sense; and I had no problem running the Java backend with JFlex/JLex and CUP.
After compiling with make build I ran, separately,

gapa@eczema:~/devel/bnfc/source/dist/build/bnfc$ ./bnfc -m --java --jflex ../../../../examples/prolog/Prolog.cf 


gapa@eczema:~/devel/bnfc/source/dist/build/bnfc$ ./bnfc -m --java ../../../../examples/prolog/Prolog.cf

and running make in each case shows that CUP got its input and processed it properly.

Could you send some more information, e.g. the command line you are using, input files, files generated etc etc

@gdetrez
Copy link
Contributor Author

@gdetrez gdetrez commented on ecf4fed Feb 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The automated test do check the java backend (and pass) and I can't reproduce this either.

@johnjcamilleri did you try re-generating the makefile (bnfc option -m)?

@johnjcamilleri
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I wasn't re-generating the Makefile with -m. But am I right that the naming of the cup file has changed recently? I came across this when trying to compile other people's code (student solutions) using their makefiles.

@gapag
Copy link

@gapag gapag commented on ecf4fed Feb 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are. I did change it and I have no documented reason for that (might have been a typo).
If needed we can revert to the old naming convention, just by modifying the cupmakedetail function in Java.hs.

Please sign in to comment.