PHP Parser for Java..?

Discussion in 'Java' started by pek, Aug 23, 2006.

  1. pek

    pek Guest

    OK, I believe I looked enough on the web (google.com, irc.freenode.net,
    google groups etc. etc.).
    I can't seem to find a single PHP Parser that I can use in Java.
    I have downloaded Apache Xerces but I believe it is not relevant (or
    I'm missing something).

    Anyway, to make it easier, this is what I want *exactly*:

    I'm developing a small program that parsers HTML files, gets only the
    PHP code and tries to recognize all PHP functions that use a PHP
    library.

    Example:
    <html>
    <?php
    mysql_connect("localhost","pek","peker");
    ?>
    </html>

    The program will parse the PHP code, recognize that "mysql_connect(*)"
    is a function, tries to find in which library the function belongs from
    an XML file which has a list of libraries and associated functions
    (this is where Xerces comes) and outputs an array of Strings with these
    informations: CodeLine, Function, Library etc.

    I simply need a PHP Parser.
    Any help/ideas/tips/suggestions/etc..?
    Thank you for your time.
    pek, Aug 23, 2006
    #1
    1. Advertising

  2. pek

    Matt Rose Guest

    pek wrote:
    > OK, I believe I looked enough on the web (google.com, irc.freenode.net,
    > google groups etc. etc.).
    > I can't seem to find a single PHP Parser that I can use in Java.
    > I have downloaded Apache Xerces but I believe it is not relevant (or
    > I'm missing something).
    >
    > Anyway, to make it easier, this is what I want *exactly*:
    >
    > I'm developing a small program that parsers HTML files, gets only the
    > PHP code and tries to recognize all PHP functions that use a PHP
    > library.
    >
    > Example:
    > <html>
    > <?php
    > mysql_connect("localhost","pek","peker");
    > ?>
    > </html>
    >
    > The program will parse the PHP code, recognize that "mysql_connect(*)"
    > is a function, tries to find in which library the function belongs from
    > an XML file which has a list of libraries and associated functions
    > (this is where Xerces comes) and outputs an array of Strings with these
    > informations: CodeLine, Function, Library etc.
    >
    > I simply need a PHP Parser.
    > Any help/ideas/tips/suggestions/etc..?
    > Thank you for your time.


    Have a look at antlr http://www.antlr.org/ and javacc
    https://javacc.dev.java.net/ then either toss a coin, take your pick,
    or try them both.

    Matt
    Matt Rose, Aug 23, 2006
    #2
    1. Advertising

  3. pek

    Moiristo Guest

    Matt Rose wrote:
    > pek wrote:
    >> OK, I believe I looked enough on the web (google.com, irc.freenode.net,
    >> google groups etc. etc.).
    >> I can't seem to find a single PHP Parser that I can use in Java.
    >> I have downloaded Apache Xerces but I believe it is not relevant (or
    >> I'm missing something).
    >>
    >> Anyway, to make it easier, this is what I want *exactly*:
    >>
    >> I'm developing a small program that parsers HTML files, gets only the
    >> PHP code and tries to recognize all PHP functions that use a PHP
    >> library.
    >>
    >> Example:
    >> <html>
    >> <?php
    >> mysql_connect("localhost","pek","peker");
    >> ?>
    >> </html>
    >>
    >> The program will parse the PHP code, recognize that "mysql_connect(*)"
    >> is a function, tries to find in which library the function belongs from
    >> an XML file which has a list of libraries and associated functions
    >> (this is where Xerces comes) and outputs an array of Strings with these
    >> informations: CodeLine, Function, Library etc.
    >>
    >> I simply need a PHP Parser.
    >> Any help/ideas/tips/suggestions/etc..?
    >> Thank you for your time.

    >
    > Have a look at antlr http://www.antlr.org/ and javacc
    > https://javacc.dev.java.net/ then either toss a coin, take your pick,
    > or try them both.


    I think it is unnecessary to create a complete php parser in java just
    to filter out the functions it contains. A simple ad-hoc algorithm would
    suffice. Just get all the php blocks from the page, and then use
    Pattern/Matcher to find all functions it contains by creating a regex.
    Moiristo, Aug 23, 2006
    #3
  4. pek

    Dave Glasser Guest

    Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    "Matt Rose" <> wrote on 23 Aug 2006 08:11:40
    -0700 in comp.lang.java.programmer:


    >Have a look at antlr http://www.antlr.org/ and javacc
    >https://javacc.dev.java.net/ then either toss a coin, take your pick,
    >or try them both.


    In an upcoming project, I'm going to have to use one of these tools
    for parsing mathematical expressions, and I have no experience with
    either of them. I'd be interested in feedback from someone who's used
    both. Is one significantly better than the other, or is a coin toss in
    fact as good a way as any for choosing one over the other?



    --
    Check out QueryForm, a free, open source, Java/Swing-based
    front end for relational databases.

    http://qform.sourceforge.net

    If you're a musician, check out RPitch Relative Pitch
    Ear Training Software.

    http://rpitch.sourceforge.net
    Dave Glasser, Aug 23, 2006
    #4
  5. pek

    Moiristo Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    Dave Glasser wrote:
    > "Matt Rose" <> wrote on 23 Aug 2006 08:11:40
    > -0700 in comp.lang.java.programmer:
    >
    >
    >> Have a look at antlr http://www.antlr.org/ and javacc
    >> https://javacc.dev.java.net/ then either toss a coin, take your pick,
    >> or try them both.

    >
    > In an upcoming project, I'm going to have to use one of these tools
    > for parsing mathematical expressions, and I have no experience with
    > either of them. I'd be interested in feedback from someone who's used
    > both. Is one significantly better than the other, or is a coin toss in
    > fact as good a way as any for choosing one over the other?


    There is a course here at the university to learn how to build a
    compiler using Antlr. I don't know about javaCC, but Antlr was quite
    easy to understand and you could very easily create a compiler for a
    custom language; you only need basic java and (E)BNF knowledge.
    Moiristo, Aug 23, 2006
    #5
  6. Moiristo wrote:
    > Matt Rose wrote:
    >
    >> pek wrote:
    >>
    >>> OK, I believe I looked enough on the web (google.com, irc.freenode.net,
    >>> google groups etc. etc.).
    >>> I can't seem to find a single PHP Parser that I can use in Java.
    >>> I have downloaded Apache Xerces but I believe it is not relevant (or
    >>> I'm missing something).


    This is not strictly on topic, but there is a php interpreter in Resin.
    I think it is in open source part, so it might help.

    DG
    =?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=, Aug 24, 2006
    #6
  7. pek

    Paul Cager Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    Moiristo wrote:
    > Dave Glasser wrote:
    >> "Matt Rose" <> wrote on 23 Aug 2006 08:11:40
    >> -0700 in comp.lang.java.programmer:
    >>
    >>
    >>> Have a look at antlr http://www.antlr.org/ and javacc
    >>> https://javacc.dev.java.net/ then either toss a coin, take your pick,
    >>> or try them both.

    >>
    >> In an upcoming project, I'm going to have to use one of these tools
    >> for parsing mathematical expressions, and I have no experience with
    >> either of them. I'd be interested in feedback from someone who's used
    >> both. Is one significantly better than the other, or is a coin toss in
    >> fact as good a way as any for choosing one over the other?

    >
    > There is a course here at the university to learn how to build a
    > compiler using Antlr. I don't know about javaCC, but Antlr was quite
    > easy to understand and you could very easily create a compiler for a
    > custom language; you only need basic java and (E)BNF knowledge.


    I've always found JavaCC easy to use (well, as easy as a compiler
    generator can be...). JavaCC is a top-down recursive descent tool which
    feels similar to a hand-crafted parser. I believe the best introduction
    to JavaCC is:

    http://www.engr.mun.ca/~theo/JavaCC-FAQ/

    I've never used ANTLR, but it seems very similar to JavaCC. I must
    reluctantly concede that ANTLR seems to be better documented.
    Paul Cager, Aug 24, 2006
    #7
  8. pek

    maaxiim Guest

    pek wrote:
    <snip>
    > Anyway, to make it easier, this is what I want *exactly*:
    >
    > I'm developing a small program that parsers HTML files, gets only the
    > PHP code and tries to recognize all PHP functions that use a PHP
    > library.
    >

    <snip>
    > I simply need a PHP Parser.
    > Any help/ideas/tips/suggestions/etc..?
    > Thank you for your time.


    Try searching on krugle.com, enter 'php parser' in the search box and
    select Java as the language. the very first hit I got was 'jhp' which
    after a superficial scan, appears to do something along the lines of
    what you require.

    regards

    maaxiim
    maaxiim, Aug 24, 2006
    #8
  9. pek

    Oliver Wong Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    "Dave Glasser" <> wrote in message
    news:...
    > "Matt Rose" <> wrote on 23 Aug 2006 08:11:40
    > -0700 in comp.lang.java.programmer:
    >
    >
    >>Have a look at antlr http://www.antlr.org/ and javacc
    >>https://javacc.dev.java.net/ then either toss a coin, take your pick,
    >>or try them both.

    >
    > In an upcoming project, I'm going to have to use one of these tools
    > for parsing mathematical expressions, and I have no experience with
    > either of them. I'd be interested in feedback from someone who's used
    > both. Is one significantly better than the other, or is a coin toss in
    > fact as good a way as any for choosing one over the other?


    Go with ANTLR. AFAIK, the JavaCC community is dead, but ANTLR is still
    being actively developed, and the lead developer participates on the mailing
    list and is open to future design suggestions.

    - Oliver
    Oliver Wong, Aug 24, 2006
    #9
  10. pek

    Paul Cager Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    Oliver Wong wrote:
    >
    > "Dave Glasser" <> wrote in message
    > news:...
    >> "Matt Rose" <> wrote on 23 Aug 2006 08:11:40
    >> -0700 in comp.lang.java.programmer:
    >>
    >>
    >>> Have a look at antlr http://www.antlr.org/ and javacc
    >>> https://javacc.dev.java.net/ then either toss a coin, take your pick,
    >>> or try them both.

    >>
    >> In an upcoming project, I'm going to have to use one of these tools
    >> for parsing mathematical expressions, and I have no experience with
    >> either of them. I'd be interested in feedback from someone who's used
    >> both. Is one significantly better than the other, or is a coin toss in
    >> fact as good a way as any for choosing one over the other?

    >
    > Go with ANTLR. AFAIK, the JavaCC community is dead, but ANTLR is
    > still being actively developed, and the lead developer participates on
    > the mailing list and is open to future design suggestions.
    >
    > - Oliver


    Well, JavaCC is still being developed (e.g. there is the Java 1.5
    version). But I agree that it is less "active" than ANTLR.
    Paul Cager, Aug 24, 2006
    #10
  11. pek

    EJP Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    Dave Glasser wrote:
    >
    > In an upcoming project, I'm going to have to use one of these tools
    > for parsing mathematical expressions, and I have no experience with
    > either of them. I'd be interested in feedback from someone who's used
    > both. Is one significantly better than the other, or is a coin toss in
    > fact as good a way as any for choosing one over the other?


    If you're really just parsing mathematical expressions, either is
    considerable overkill. A tiny recursive-descent parser will be quite
    adequate.
    EJP, Aug 25, 2006
    #11
  12. pek

    Paul Cager Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    EJP wrote:
    > Dave Glasser wrote:
    >>
    >> In an upcoming project, I'm going to have to use one of these tools
    >> for parsing mathematical expressions, and I have no experience with
    >> either of them. I'd be interested in feedback from someone who's used
    >> both. Is one significantly better than the other, or is a coin toss in
    >> fact as good a way as any for choosing one over the other?

    >
    > If you're really just parsing mathematical expressions, either is
    > considerable overkill. A tiny recursive-descent parser will be quite
    > adequate.


    Good point. In fact, do you need a parser at all; would a scripting
    engine like BeanShell work?
    Paul Cager, Aug 25, 2006
    #12
  13. pek

    Dave Glasser Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    EJP <> wrote on Fri, 25 Aug 2006
    08:01:07 GMT in comp.lang.java.programmer:

    >Dave Glasser wrote:
    >>
    >> In an upcoming project, I'm going to have to use one of these tools
    >> for parsing mathematical expressions, and I have no experience with
    >> either of them. I'd be interested in feedback from someone who's used
    >> both. Is one significantly better than the other, or is a coin toss in
    >> fact as good a way as any for choosing one over the other?

    >
    >If you're really just parsing mathematical expressions, either is
    >considerable overkill. A tiny recursive-descent parser will be quite
    >adequate.


    Which one do you recommend?
    Dave Glasser, Aug 26, 2006
    #13
  14. pek

    Dave Glasser Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    Paul Cager <> wrote on Fri, 25 Aug 2006 22:48:45
    GMT in comp.lang.java.programmer:

    >EJP wrote:
    >> Dave Glasser wrote:
    >>>
    >>> In an upcoming project, I'm going to have to use one of these tools
    >>> for parsing mathematical expressions, and I have no experience with
    >>> either of them. I'd be interested in feedback from someone who's used
    >>> both. Is one significantly better than the other, or is a coin toss in
    >>> fact as good a way as any for choosing one over the other?

    >>
    >> If you're really just parsing mathematical expressions, either is
    >> considerable overkill. A tiny recursive-descent parser will be quite
    >> adequate.

    >
    >Good point. In fact, do you need a parser at all; would a scripting
    >engine like BeanShell work?


    I think it would be difficult to adapt BeanShell to this particular
    problem.

    --
    Check out QueryForm, a free, open source, Java/Swing-based
    front end for relational databases.

    http://qform.sourceforge.net

    If you're a musician, check out RPitch Relative Pitch
    Ear Training Software.

    http://rpitch.sourceforge.net
    Dave Glasser, Aug 26, 2006
    #14
  15. pek

    Oliver Wong Guest

    Re: Antlr vs. JavaCC: Opinions? (Was Re: PHP Parser for Java..?)

    "Dave Glasser" <> wrote in message
    news:...
    > EJP <> wrote on Fri, 25 Aug 2006
    > 08:01:07 GMT in comp.lang.java.programmer:
    >
    >>Dave Glasser wrote:
    >>>
    >>> In an upcoming project, I'm going to have to use one of these tools
    >>> for parsing mathematical expressions, and I have no experience with
    >>> either of them. I'd be interested in feedback from someone who's used
    >>> both. Is one significantly better than the other, or is a coin toss in
    >>> fact as good a way as any for choosing one over the other?

    >>
    >>If you're really just parsing mathematical expressions, either is
    >>considerable overkill. A tiny recursive-descent parser will be quite
    >>adequate.

    >
    > Which one do you recommend?


    I think the OP is telling you to write one from scratch.

    - Oliver
    Oliver Wong, Aug 28, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ted Zeng
    Replies:
    4
    Views:
    489
    Tim Arnold
    Oct 3, 2006
  2. raviraj joshi
    Replies:
    0
    Views:
    312
    raviraj joshi
    Jul 4, 2009
  3. reshma shinde
    Replies:
    0
    Views:
    402
    reshma shinde
    Jul 4, 2009
  4. Rajive Narain
    Replies:
    0
    Views:
    1,668
    Rajive Narain
    Sep 18, 2009
  5. raviraj joshi
    Replies:
    0
    Views:
    165
    raviraj joshi
    Jul 4, 2009
Loading...

Share This Page