0

I am working on a small project on my compiler subject and was assigned on the switch-case(java) part.So here's how it goes:A user would enter a code snippet from a switch-case statement,and my program should be able to identify if the code from the user has the right syntax,or if it is working.It's kinda hard to explain so here is an example: A user would input this

   int try=1;
   switch (try){
        case 1:
            System.out.print("try=1 /or anything/");
            break;
        case 2:
            System.out.print("try=2 /or anything/");
            break;
        }
   }

and my program should output the result from the case statement,in this case,that "try=1" should be the output.The contents of the case doesn't really matter,but my code should be able to use that as MY output.And,it should be able to tell if the user-inputted code is not working,or has some syntax error.For example,if the variable was not initialized,my output should be "Identifier not initialized" or something like that.Is there any library or something that could help me with this?I tried coding this manually and tried tokenizing inputs,but it's really hard and very long,as the possibilities are endless because the user could also input a small operation too before using the variable in the switch case.I'm currently trying to understand the ANTLR which my prof had suggested.If any of you could give me any link or provide any help with this I would be really grateful.I am not asking for codes,just a little help on what to look for as I really dont know what to search.I'm sorry if this is a very long post and quite a noob question to some of you(I'm not really good at this),but thanks a lot for reading.Any help is appreciated :D

Jens
  • 67,715
  • 15
  • 98
  • 113
  • @WillShackleford: I would imagine that the intent is to actually *execute* the code to observe its output more than anything else, so I'd say it's fine to use syntactically valid Java here. – Makoto Oct 17 '15 at 15:59
  • do you want to add `default` case for invalid input? – Saravana Oct 17 '15 at 17:14
  • oh yeah i forgot the default sorry about that.the main objective is to actually see if the input is valid when it comes to syntax and check whether it is executable or not. – Christine Gem Nazareno Oct 17 '15 at 17:50

0 Answers0