Page 1 of 2 12 LastLast
Results 1 to 20 of 31
  1. #1
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default how to make a simple language

    Ok I am trying to make a very very simple computer language. Basically I have a console in which the user types in, and the program then searches for the words to see if they are command words. The command words number only a few, and they can be found in an array. For example if I type in "clear", which is a command word, a method will return if what the user typed in is in fact a command word by searching through an array of command words. The problem I am running into is what to do when I encounter "()" on the same line. I am hoping to search for Parenthesis in another array but I am not sure how to search for both the command and the parenthesis. When my program looks for "clear()", incidentally it cannot find it because it is looking for both "clear" and "()" in the same array. I want my program to look for "clear" first, check it, and then look for "()" and check that. How can this be accomplished.
    P.S here is some of my code, note when I am just looking for one word in one array that works fine. I just can't look for one word in one array and then look for some special characters in another array:

    Java Code:
    public static String[] commands={"MakeStuff", "clear", "move", "skip", "exit", "stop"};
    public static final String[] puncs={"()",";","[","]","{","}","(",")","()","\"","'", "!"};
    
    public static boolean isCommand(String cmd){
            
            for (int x = 0; x < puncs.length; x++){
                if(cmd.equals(commands[x])){
                    return true;
                }
            }
            return false;
        }
        
        public static boolean isPunc(String punc){
            for (int x = 0; x < puncs.length; x++){
                if(punc.equals(puncs[x])){
                    //System.out.println("()");
                    return true;
                }
            }
            return false;
        }

  2. #2
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    To see the application of all that you can refer to this:

    Java Code:
    Dictionary dic = new Dictionary();
    String line = readLine("Type here when ready: ");
     if(dic.isCommand(line)){
    println(line +"found")  //for testing purposes, this works
    //doSomething()
    }
    I can also say dic.isPunc(line) and I will get true if all that is type in is "()". I just want to be able to include both methods in same way

  3. #3
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    It is also worth noting that when I search for "clear()", I get an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    I know my command array is not equipped to deal with "()" and some might just say to include the "()" in the command array of words but that doesn't help my long term goals. My next step after confirming to see if there are "()" would be then be to check a list of objects inside the "()". Consequently, if I were to say clear(trees) in the console, then that would clear some trees in a graphics program.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,661
    Rep Power
    5

    Default Re: how to make a simple language

    Do you commands always have () associated with them? Can you provide a small example of a series of
    commands in your language?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to make a simple language

    First design the grammar for your language; next implement its parser.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    I am trying to make a lexer and parser but I am unsure of where to start. An example of a program identifying a function and an object would be extremely helpful(that is all my language is, functions with objects inside the parenthesis). So something like clear(tree) would be a regular expression in my langauge. A line would always consist of a command and an object, anything else is an error.
    To answer the last question, my commands always have () attached to them.

  7. #7
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    My grammer is simply this:
    <Command> ::= <Paren><Name><Paren>
    <Name> ::= <String> //the String will be checked to see if it belongs to a list of stuff, for example a house, etc.

    I am not entirely sure how to implement the lexer but the parser will have these methods
    parseCommand()
    parseParen()
    parseName()

    Any help in filling in the details would help me greatly

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,661
    Rep Power
    5

    Default Re: how to make a simple language

    If you language is simple, I would recommend looking at regular expressions and employing capture groups.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to make a simple language

    Quote Originally Posted by Snake006 View Post
    My grammer is simply this:
    <Command> ::= <Paren><Name><Paren>
    <Name> ::= <String> //the String will be checked to see if it belongs to a list of stuff, for example a house, etc.

    I am not entirely sure how to implement the lexer but the parser will have these methods
    parseCommand()
    parseParen()
    parseName()

    Any help in filling in the details would help me greatly
    That can't be the grammar, i.e. it accepts '(foo)' (the command doesn't have a name). Probable the first grammar rule should be:

    <Command> ::= <Name><LParen><Name><RParen>

    (note that I changed the 'Paren' token to LParen and RParen, otherwise the rule would accept 'foo)bar(' etc.) This simple grammar can easily be parsed by a regular expression: [a-z]+\([a-z]+\)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    Ok now I'm getting somewhere. Thanks. I will probably need more help down line but this gets me off to a good start

  11. #11
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    One thing though, the first String cannot accept \ and claims it is an illegal escape character. Would this do the trick?:
    private static final String idPatternRE = "[a-zA-Z][a-zA-Z0-9_]*";

  12. #12
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    Finally once I have a pattern to check for how do I check for the Names separately. As stated earlier, each name belongs to one or more array of words. I am hoping once my program looks for 'c' 'l ''e' 'a' 'r' , it knows that it is a command word. Then once it checks for both '(' ')', I am not sure how to go backwards from the last paren. I need to check the name inside the parenthesis in order to compare the object with an array list of stuff. Once both names are found then I can just implement some methods for functionally and I should be home free then.

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,661
    Rep Power
    5

    Default Re: how to make a simple language

    You need to use a double back slash - example \\(. The first one escapes the second \ in the string so that \( is passed to the regex
    engine.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    ok, I will try that. If it helps at all I can show you all my parse class, lex class, name, and symbol class. I'm pretty sure I'm a little off on each one.

  15. #15
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    Hopefully I am overthinking it too. I hope something as simple as looking for 3 distinct entries wouldn't require a tremendous amount of code.

  16. #16
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    Ok, nevermind about what I said before. I made some more progress. So I can use something like this:
    if(line.matches(dic.commandPattern)){
    isCommand(true)
    }
    To return if the word is a command word(basically it is a command word as long as it doesn't contain parenthesis).
    My next question, and hopefully one of the last, is after that what would be a regular expression for parenthesis and something inside the parenthesis be?

  17. #17
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,971
    Rep Power
    8

    Default Re: how to make a simple language

    Check out the Java Pattern API for help in creating regular expressions: Pattern (Java Platform SE 7 )

    Post what you've tried and what it gets wrong, and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #18
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    It should also be noted that the line.matches is checking the entire string. I would like only to check part of the string first. So basically if I type in 'clear' it knows it should be a command word(I will check for what kind of command word later, although with this string pattern abcd would be considered a command word). But if I have something like abc(), it of course doesn't not return true because it is checking the entire string. I want to be able to check everything preceding the () first. I won't to be able to say something like this:
    while(!line.contains("(")){
    if(line.matches(dic.idPatternRE)){
    dic.isCommand(line);
    //System.out.println("true");
    }
    }
    But something like this of course will only return true infinitely if the string doesn't have a () at all. So basically I am struck at this part.

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,661
    Rep Power
    5

    Default Re: how to make a simple language

    You still have not defined a specific grammar so it is difficult to help you (or for you to program the parser).
    For example, what is the most complex line of your language? What is the least complex? How many
    keywords to you have? To help you specify something from which you can work you may want to
    read up on Backus-Naur Form - Wikipedia, the free encyclopedia. Also, regular expressions
    can easily parse something with or without parentheses and even grab something within the parentheses.
    They can do this very easily.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: how to make a simple language

    A line will be no more complex than something like clear(tree) and it also won't be any simpler because I am not allowing for empty parameters. So the grammar will always be
    <CommandWord>'('<Object>')'
    Anything else is an error. My commandWords only deal with modifying objects in a graphical program. So if I say makeStuff(house), it will make a house appear onscreen. I can never say makeStuff() because there would be nothing to modify. So basically I just want to grab something outside the parenthesis and something inside the parenthesis but I'm not sure how to do both at the same time.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 12-21-2012, 11:15 PM
  2. Trying to make simple calculator
    By Buldogg in forum New To Java
    Replies: 1
    Last Post: 09-18-2011, 01:37 PM
  3. how to make a simple panel??
    By stefandanielsen in forum New To Java
    Replies: 10
    Last Post: 05-17-2011, 02:12 PM
  4. make simple web service
    By BigBear in forum Java Servlet
    Replies: 2
    Last Post: 06-13-2010, 12:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •