Java Lexical Analyzer -
i'm creating lexical analyzer. in code can output symbols, when comes letters , numbers cant design code in that..
please me in project.. thanks
package lab7; import java.util.scanner; import java.util.stringtokenizer; public class lab7 { public static void main(string[] args) { string word; char[] wordarray; scanner sc = new scanner(system.in); wordarray = word.tochararray(); for(int x = 0; x < wordarray.length; x++) { if(wordarray[x] == '+') { system.out.print("add "); } else if(wordarray[x] == '-') { system.out.print("subtract "); } else if(wordarray[x] == '*') { system.out.print("multiply "); } else if(wordarray[x] == '/') { system.out.print("divide "); } else if(wordarray[x] == '(') { system.out.print("math "); } else if(wordarray[x] == ')') { system.out.print("math "); } else if(wordarray[x] == '=') { system.out.print("assign "); } } } }
character methods character.isletter , character.isdigit may here.
however can't write real lexical analyzer scanning string letter letter. lexical analyzer produces multi-character tokens , implemented state machine, defined regular expressions.
automatic splitting isn't useful here. example, if you'll split input string whitespaces, 12+34 single token. should define rules, example, read digits, while there digits, , on.. leads idea of a finite automaton (or state machine).
Comments
Post a Comment