Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8606801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:11:10+00:00 2026-06-12T03:11:10+00:00

I have a simple language which consists of patterns like size(50*50) start(10, 20, -x)

  • 0

I have a simple language which consists of patterns like

size(50*50)
start(10, 20, -x)
forward(15)
stop

It’s an example of turtle-drawing language. I need to properly tokenize it. The above is a source code instance. Statements and expressions are separated with newlines. I set up my Scanner to use delimiters like newlines. I expect next("start") to eat the string “start”, and then I issue next("(") to eat the first parenthesis. It appears however, that it does something else than I expect. Has the scanner already broken the above into tokens based on delimiter and/or do I need to approach this differently? For me, “start”, “(“, “50”, “*”, “50” and “)” on the first line would constitute separate tokens, which appears to be an unfulfilled expectation here. How can I tokenize the above with as little code as possible? I don’t currently need to write a tokenizer, I am writing an interpreter, so tokenizing is something I don’t want to spend my time on currently, I just like Scanner to work with me here.

My useDelimiter call is as follows:

Scanner s ///...
s.useDelimiter(Pattern.compile("[\\s]&&[^\\r\\n]"));

Issuing first next call gives me the entire file contents. Without the above call, it gives me entire first line.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:11:12+00:00Added an answer on June 12, 2026 at 3:11 am

    To write a proper parser, you need to define your language in a formal grammar. Trust me, you want to do it properly or you will have problems downstream.

    You can probably represent your tokens as regular expressions at the lowest level, but first you need to be clear about your grammar, which is combinations of tokens in lexical structures. You can represent this as recursive functions (methods), known as Productions. Each Production function can use scanner to test whether or not it is looking at a token it wants. But scanner will consume the input and you can’t reverse.

    If you used Scanner, you will find the following things unsuitable:

    1. It will always parse a token according to the regular expression,

      1.1 so even if you do get a token you can use, you will have to write more code to decide exactly what token it was

      1.2 and you may not be able to represent your language grammar as one big expression

    2. You can’t re-wind. A look-ahead parser (required for lots of grammars like yours) needs to be able to look ahead at the input stream and then decide, if it wants, not to use the input and to let another token parser function use it.

    I suggest you write the character lexer yourself, and iterate over a string / array of chars rather than a stream. Then you can re-wind.

    Otherwise, use a ready-built lexer/parser framework like yacc or Coco/R.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to try out the D programming language. I have simple pet project
I have a simple page which allows users to add language ids to items.
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have simple class with width and height member fields which define number of
Hello I want to make something like a meta language which gets parsed and
I have a stupid question :) I'm working on a simple php document which
Haskell is a pure functional language, which means Haskell functions have no side affects.
I am using Lex/Bison to create a simple scripting language which translates into C.
I have a conceptual question. Suppose I have a procedure (any language) which takes
I'm using Jison to build a simple calculator language, which includes variables. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.