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 9228703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:23:24+00:00 2026-06-18T05:23:24+00:00

I don’t know if this info is relevant to the question, but I am

  • 0

I don’t know if this info is relevant to the question, but I am learning Scala parser combinators.
Using some examples (in this master thesis) I was able to write a simple functional (in the sense that it is non imperative) programming language.

Is there a way to improve my parser/evaluator such that it could allow/evaluate input like this:

<%
import scala.<some package / classes>
import weka.<some package / classes>
%>

some DSL code (lambda calculus)

<%
System.out.println("asdasd");
J48 j48 = new J48();
%>

as input written in the guest language (DSL)?

Should I use reflection or something similar* to evaluate such input?
Is there some source code recommendation to study (may be groovy sources?)?

Maybe this is something similar: runtime compilation, but I am not sure this is the best alternative.

EDIT

Complete answer given bellow with “{” and “}”. Maybe “{{” would be better.

  • 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-18T05:23:26+00:00Added an answer on June 18, 2026 at 5:23 am

    I managed how to run Scala code embedded in my interpreted DSL.

    Insertion of DSL vars into Scala code and recovering returning value comes as a bonus. 🙂

    Minimal relevant code from parsing and interpreting until performing embedded Scala code run-time execution (Main Parser AST and Interpreter):

    object Main extends App {
         val ast = Parser1 parse "some dsl code here"
         Interpreter eval ast
    }
    
    object Parser1 extends RegexParsers with ImplicitConversions {
      import AST._
      val separator = ";"
      def parse(input: String): Expr = parseAll(program, input).get
      type P[+T] = Parser[T]
      def program = rep1sep(expr, separator) <~ separator ^^ Sequence
      def expr: Parser[Expr] = (assign /*more calls here*/)
      def scalacode: P[Expr] = "{" ~> rep(scala_text) <~ "}" ^^ {case l => Scalacode(l.flatten)}
      def scala_text = text_no_braces ~ "$" ~ ident ~ text_no_braces ^^ {case a ~ b ~ c ~ d => List(a, b + c, d)}
      //more rules here
      def assign = ident ~ ("=" ~> atomic_expr) ^^ Assign
      //more rules here
      def atomic_expr = (
         ident ^^ Var
            //more calls here 
            | "(" ~> expr <~ ")"
            | scalacode
            | failure("expression expected")
         )
      def text_no_braces = """[a-zA-Z0-9\"\'\+\-\_!@#%\&\(\)\[\]\/\?\:;\.\>\<\,\|= \*\\\n]*""".r //| fail("Scala code expected")
      def ident = """[a-zA-Z]+[a-zA-Z0-9]*""".r
    }
    
    object AST {
       sealed abstract class Expr
       // more classes here
       case class Scalacode(items: List[String]) extends Expr
       case class Literal(v: Any) extends Expr
       case class Var(name: String) extends Expr
    }
    
    object Interpreter {
      import AST._
      val env = collection.immutable.Map[VarName, VarValue]()
      def run(code: String) = {
         val code2 = "val res_1 = (" + code + ")"
         interpret.interpret(code2)
         val res = interpret.valueOfTerm("res_1")
         if (res == None) Literal() else Literal(res.get)
      }
    
      class Context(private var env: Environment = initEnv) {
        def eval(e: Expr): Any = e match {
           case Scalacode(l: List[String]) => {
              val r = l map {
                 x =>
                    if (x.startsWith("$")) {
                       eval(Var(x.drop(1)))
                    } else {
                       x
                    }
              }
              eval(run(r.mkString))
           }
           case Assign(id, expr) => env += (id -> eval(expr))
           //more pattern matching here
           case Literal(v) => v
           case Var(id) => {
              env getOrElse(id, sys.error("Undefined " + id))
           }
         }
        }  
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know why this is happening, but after submitting a form via JS (using
Don't know if this is possible, but I have some code like this: val
I don't know why, but this code worked for me a month ago... maybe
Don't know if I'm using the not selector incorrectly but I want everything clicked
Don't know if this is the right place to ask this, but I will
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if I'm over-thinking this or not.. but I'm trying to be able
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
Don't know why but I can't find a solution to this. I have 3
don't know better title for this, but here's my code. I have class user

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.