-3

I would like to define a set of coding rules of a language structural proprietary language,

how can I proceed? thank you very much

terre
  • 15
  • 7
  • Do you want to define a set of coding rules? Or do you want to implement a program that will check whether a set of coding rules is followed? If the latter, JavaCC will be of help. If the former, JavaCC might become your new best friend. Please clarify. However I suspect that even if you clarify the question, it will still be too broad for stackoverflow. – Theodore Norvell Mar 23 '15 at 12:03
  • Hello, Thank you for the rsponse. after the definition of some coding rules, for example: 1-first encoding rule is the maximum number of "if nested" is 4. 2-second coding rules each file should not exceed 150 lines. my question is: what are the advantages of JavaCC compared to other parser generators like (Flex / Bison, ANTLR, COCO / R, grammatica, .....) thank you very much – terre Mar 23 '15 at 23:04
  • in fact, I want to develop a parser that can detect and check is that all files coded in proprietary language XX, respect such coding conventions for exemple : is that the functions do not exceed 100 lines, or the method not exceed 50 lines? for this, I generated my parser by JavaCC tool; My question: How can I programmes the rule (for exemple to detected the function what have the nb of lines=100 or the method that have nb = 50 lines) , with regular expressions and the grammar of proprietary language . thank you – terre Mar 28 '15 at 16:39
  • You are asking a lot of different question here. First off you want to know about the advantages of JavaCC compared to many other parser generators. That's way too broad for SO. Second you want to know how to program a bunch of rules of which you give one two examples. Again way too broad to be answered specifically. I'll answer in broad terms. – Theodore Norvell Mar 29 '15 at 01:30

2 Answers2

0

Some of your rules will be easy to deal with using only parsing. For example to find the number of lines in a function is not hard:

void function() : {
    int firstLine, lastLine ; } 
{
    {firstLine = getToken(1).beginLine ; }
    ...
    {lastLine = getToken(0).beginLine ; }
    {check( lastLine - firstLine + 1 <= 150 ) ; }
}

Other rules, such as your example of nested ifs, will be best handled by traversing an abstract syntax tree. I'd suggest you look into JJTree. It supports visitors, which could be quite useful for encoding some of your rules.

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
  • hello, Thank you for your response. I would like to define the grammar of My language to start coding the coding convention rules (such a method should not have 50 lines). my question : what is the grammar of my langage is like this: deferent type varaible as (string, collections, integer, ....). structural loops like (IF () THEN) ... I want to program with JavaCC tool; is what the syntax is linked with JavaCC grammar.? – terre Mar 29 '15 at 14:57
  • "is what the syntax is linked with JavaCC grammar?" Can you clarify the question? – Theodore Norvell Mar 29 '15 at 19:36
  • Thank you for your response. in the fichier .jj , I have to declare the grammar of my language. and then encode the rule (a method not exceed 50 lines). tool used to generate the parser is JavaCC. my question how I will declare my language grammar. for exemple : the type of variables is : String, Collection, Bolean. Thank you – terre Mar 29 '15 at 21:58
  • I'm going to assume you are familiar with EBNF grammars and regular expressions, If not the first step is to get familiar with them. You write your lexer and parser descriptions in JavaCC syntax in a .jjt file. Lots of documentation and tutorials around on how to do that. Then run the jjt file through JJTree and the resulting jj file through JavaCC and out comes a lexer, a parser, and a tree representation. Write your rules as code that traverses the tree, possibly using visitors. Depending on the rules, you may need more infrastructure such as a symbol table. That's my advice. Good luck. – Theodore Norvell Mar 29 '15 at 22:45
  • Thank you for your response. If I understood , I set the firt my grammar in .jj file , after, I compile with JavaCC the .jj the file, then there is creationsof .java files (included my parser file). my question, I'm going coding the rules (eg, a method must spend 50 lines) in what file location ( .jj file? or .java file). Another question: what difference between JJTree, JTB and JavaCC? Thank you very much – terre Apr 01 '15 at 08:29
  • JavaCC turns lexer specifications into lexers and grammar specifications into parsers. You can add Java code that will be incorporated into the parser; usually this is for generating an intermediate representation such as an abstract syntax tree. JJTree is a preprocessor that generates the additional Java code to make an abstract syntax tree. JTB is similar. My advice is use JJTree and code the rules as Java classes that inspect the AST. – Theodore Norvell Apr 03 '15 at 11:14
  • Thank you for your response. If I understood , I set the firt my grammar in .jj file – terre Apr 07 '15 at 21:02
  • Thank you for your response. If I understood , in the firt, I set my grammar in .jjt file. after, I'll encode < encoding rules : for exemple List my methods are not calling> in the java classes?. Thank you very much – terre Apr 07 '15 at 21:08
  • I have a question about formel grammar, how I can translate my formel grammar into javacc grammar. Thank you for – terre Apr 09 '15 at 09:13
  • Three simple steps: (0) Learn JavaCC by reading some combination of Tom Copeland's book, the documentation, the FAQ. If needed supplement this with a study of grammars and parsing; e.g., by reading Aho, Ullman, Sethi, and Lam. (1) Translate the grammar to JavaCC. (2) Test carefully. If this seems too hard, hire a consultant. – Theodore Norvell Apr 09 '15 at 14:02
  • Thank you very much for your answer. I have a question about a grammar left-recursive. Is the following grammar productions are included in a grammar left recursive : A := ( B|C|D) C:= ( E|F|G) E:= ( H , A) . Thank you very much – terre Apr 10 '15 at 14:32
  • If it were left recursive JavaCC would warn you. – Theodore Norvell Apr 10 '15 at 14:41
  • Thank you very much for your answer. How can I change my grammar be accepted by javacc. Thank you – terre Apr 10 '15 at 16:07
  • Sorry, but look: this is not what Stack Exchange is for. If you have a question, do your own research to solve it, and, if that doesn't work, post a question (not a comment) on SO asking your question, supported by what you have tried yourself. But, I would advise that, before you ask any more questions about JavaCC, you take my advice from a few comments back and read the documentation, the FAQ, Tom Copeland's book and a decent book on parsing or compiling. For anyone with a good grasp of parsing theory, JavaCC is easy to learn, otherwise the best advice is to learn a bit of theory first. – Theodore Norvell Apr 10 '15 at 20:40
  • Hello, I coded the rule to find the number of lines in a function. but, I have un SPECIAL_TOKEN : { < COMMENT: "//"(~["\n","\r"])* ("\n"|"\r"|"\r\n")? > }. so the ligne of comments is inclu in the number of lines in a function. thank you very much. – terre Jun 18 '15 at 12:10
  • Hello, I coded the rule to find the number of lines in a function Like this : void function() : { int firstLine, lastLine ; } { {firstLine = getToken(1).beginLine ; } ... {lastLine = getToken(0).beginLine ; } {check( lastLine - firstLine + 1 <= 150 ) ; } }. but, I have in .jj file, the SPECIAL_TOKEN : { < COMMENT: "//"(~["\n","\r"])* ("\n"|"\r"|"\r\n")? > }. so the comment lines are included in the total lines of the function. How I can fix this bug. thank you very much. – terre Jun 18 '15 at 12:16
  • I can't see why that would be. getToken should return only regular tokens, not special tokens. My advice is to print the `.kind` field from those tokens. I expect you'll see that they are regular tokens. If not post a comment, cause I'd be interested to find out. – Theodore Norvell Jun 18 '15 at 12:21
  • I did not understand your advice. thank you very much – terre Jun 18 '15 at 15:54
  • Hello, I have like in put file is : function name() //statement1 statement2 //statement3 statement4 //statement5 statement6 end_function. in file .jj thise line of code : void function() : { int firstLine, lastLine ; } { {firstLine = getToken(1).beginLine ; System.out.println("getToken(1) = " + getToken(1)); } Statement() {lastLine = getToken(0).beginLine ; System.out.println("getToken(0) = " + getToken(0)); }. I have in output of the standard exit like : getToken(1) = statement2 getToken(0) = statement6 moreover I have the line of function is = 5 LINES. thank you – terre Jun 19 '15 at 13:42
  • Instead of `firstLine = getToken(1).beginLine ;` put `tk=getToken(1) ; firstLine = tk.beginLine ; System.out.println("First token is a "+tokenImage[tk.kind]);` Similarly for the the lastLine. I think you'll find that the first and last token are not special tokens. – Theodore Norvell Jun 19 '15 at 13:47
  • So what you want to count is lines that don't consist solely of comments? Figure out exactly what you want to count and POST A QUESTION. As I said above, this --asking questions in comments-- is not how stack exchange works. If you want an answer to a question post the question. Be absolutely clear about what problem you need to solve and what you have done your self to solve it. – Theodore Norvell Jun 19 '15 at 13:58
  • I find that the first and last token are regular tokens – terre Jun 19 '15 at 14:19
0

If I have understood correctly, in the file .jj, I have to add this function , which allows to check how many line of code in a method

or in the file parseur.java generated by javacc.

terre
  • 15
  • 7