简体   繁体   中英

String Searching

Okay, I'm writing a language compiler in C# that converts my language into MSIL and then calls the MSIL compiler. But that's beside the point. I created some code to show you want I'm going to be working with:

module myApplication

  [EntryPoint]
  function void start
    write("Hello World!\n")
    wait(5) // wait 5 seconds
    ret()
  endfunction
endModule   

Okay, I'm trying to scan through that code and find where "[EntryPoint]" is and then find all the "function void start" after it. So I can find the function type ("void") and name ("start"). The [EntryPoint] tag will always be above the function that will be defined as the applications entry point. And I will have to make it trim the whitespace and stuff. This is also scanning through using '\\n' so the function MUST be under the tag.

And I also have another question, how would I make it separate the code inside the function from "function void start" and "endfunction"?

There is a lot more to writing a compiler than simple string manipulation - for example what if your source instead looked like this:

module myApplication
  string myString = "[EntryPoint]
  function void start
    write("Hello World!\n")
    wait(5) // wait 5 seconds
    ret()
  endfunction"
endModule

Ie your source defines a string containing the source for a program - you can't just look through for something that looks like an entry point, you need to actually parse your source.

See Learning to write a compiler for good resources on getting started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM